source: josm/trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImageryHandler.java @ 4883

Last change on this file since 4883 was 4861, checked in by bastiK, 11 years ago

remotecontrol: add 'type' parameter for /imagery request (this should be extended further)

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.remotecontrol.handler;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.UnsupportedEncodingException;
7import java.net.URLDecoder;
8import java.util.HashMap;
9import java.util.StringTokenizer;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.imagery.ImageryInfo;
13import org.openstreetmap.josm.gui.layer.ImageryLayer;
14import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
15
16public class ImageryHandler extends RequestHandler {
17    public static final String command = "imagery";
18    public static final String permissionKey = "remotecontrol.permission.imagery";
19    public static final boolean permissionDefault = true;
20
21    @Override
22    public String getPermissionMessage() {
23        return tr("Remote Control has been asked to load an imagery layer from the following URL:") +
24        "<br>" + args.get("url");
25    }
26
27    @Override
28    public String[] getMandatoryParams()
29    {
30        return new String[] { "url" };
31    }
32
33    @Override
34    public PermissionPrefWithDefault getPermissionPref()
35    {
36        return new PermissionPrefWithDefault(permissionKey, permissionDefault,
37        "RemoteControl: import forbidden by preferences");
38    }
39
40    @Override
41    protected void handleRequest() throws RequestHandlerErrorException {
42        if (Main.map == null) //Avoid exception when creating ImageryLayer with null MapFrame
43            throw new RequestHandlerErrorException();
44        String url = args.get("url");
45        String title = args.get("title");
46        String type = args.get("type");
47        if((title == null) || (title.length() == 0))
48        {
49            title = tr("Remote imagery");
50        }
51        String cookies = args.get("cookies");
52        ImageryLayer imgLayer = ImageryLayer.create(new ImageryInfo(title, url, type, null, cookies));
53        Main.main.addLayer(imgLayer);
54    }
55
56    @Override
57    public void parseArgs() {
58        StringTokenizer st = new StringTokenizer(request, "&?");
59        HashMap<String, String> args = new HashMap<String, String>();
60        // skip first element which is the command
61        if(st.hasMoreTokens()) {
62            st.nextToken();
63        }
64        while (st.hasMoreTokens()) {
65            String param = st.nextToken();
66            int eq = param.indexOf("=");
67            if (eq > -1)
68            {
69                String key = param.substring(0, eq);
70                /* "url=" terminates normal parameters
71                 * and will be handled separately
72                 */
73                if("url".equals(key)) {
74                    break;
75                }
76
77                String value = param.substring(eq + 1);
78                // urldecode all normal values
79                try {
80                    value = URLDecoder.decode(value, "UTF-8");
81                } catch (UnsupportedEncodingException e) {
82                    // TODO Auto-generated catch block
83                    e.printStackTrace();
84                }
85                args.put(key,
86                        value);
87            }
88        }
89        // url as second or later parameter
90        int urlpos = request.indexOf("&url=");
91        // url as first (and only) parameter
92        if(urlpos < 0) {
93            urlpos = request.indexOf("?url=");
94        }
95        // url found?
96        if(urlpos >= 0) {
97            // URL value
98            String value = request.substring(urlpos + 5);
99            // allow skipping URL decoding with urldecode=false
100            String urldecode = args.get("urldecode");
101            if((urldecode == null) || (Boolean.valueOf(urldecode) == true))
102            {
103                try {
104                    value = URLDecoder.decode(value, "UTF-8");
105                } catch (UnsupportedEncodingException e) {
106                    // TODO Auto-generated catch block
107                    e.printStackTrace();
108                }
109            }
110            args.put("url", value);
111        }
112        this.args = args;
113    }
114}
Note: See TracBrowser for help on using the repository browser.