1 | package org.openstreetmap.josm.io;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.io.InputStream;
|
---|
6 | import java.io.InputStreamReader;
|
---|
7 | import java.io.OutputStreamWriter;
|
---|
8 | import java.io.StringWriter;
|
---|
9 | import java.io.Writer;
|
---|
10 | import java.net.HttpURLConnection;
|
---|
11 | import java.net.URL;
|
---|
12 | import java.net.UnknownHostException;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.LinkedList;
|
---|
15 |
|
---|
16 | import org.jdom.JDOMException;
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
22 | import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
|
---|
23 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Class that uploades all changes to the osm server.
|
---|
27 | *
|
---|
28 | * This is done like this: - All objects with id = 0 are uploaded as new, except
|
---|
29 | * those in deleted, which are ignored - All objects in deleted list are
|
---|
30 | * deleted. - All remaining objects with modified flag set are updated.
|
---|
31 | *
|
---|
32 | * This class implements visitor and will perform the correct upload action on
|
---|
33 | * the visited element.
|
---|
34 | *
|
---|
35 | * @author imi
|
---|
36 | */
|
---|
37 | public class OsmServerWriter extends OsmConnection implements Visitor {
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * This list contain all sucessfull processed objects. The caller of
|
---|
41 | * upload* has to check this after the call and update its dataset.
|
---|
42 | *
|
---|
43 | * If a server connection error occours, this may contain fewer entries
|
---|
44 | * than where passed in the list to upload*.
|
---|
45 | */
|
---|
46 | public Collection<OsmPrimitive> processed;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Whether the operation should be aborted as soon as possible.
|
---|
50 | */
|
---|
51 | private boolean cancel = false;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Send the dataset to the server. Ask the user first and does nothing if he
|
---|
55 | * does not want to send the data.
|
---|
56 | */
|
---|
57 | public void uploadOsm(Collection<OsmPrimitive> list) throws JDOMException {
|
---|
58 | processed = new LinkedList<OsmPrimitive>();
|
---|
59 | initAuthentication();
|
---|
60 |
|
---|
61 | progress.setMaximum(list.size());
|
---|
62 | progress.setValue(0);
|
---|
63 |
|
---|
64 | NameVisitor v = new NameVisitor();
|
---|
65 | try {
|
---|
66 | for (OsmPrimitive osm : list) {
|
---|
67 | if (cancel)
|
---|
68 | return;
|
---|
69 | osm.visit(v);
|
---|
70 | currentAction.setText("Upload "+v.className+" "+v.name+" ("+osm.id+")...");
|
---|
71 | osm.visit(this);
|
---|
72 | progress.setValue(progress.getValue()+1);
|
---|
73 | }
|
---|
74 | } catch (RuntimeException e) {
|
---|
75 | throw new JDOMException("An error occoured: ", e);
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Upload a single node.
|
---|
81 | */
|
---|
82 | public void visit(Node n) {
|
---|
83 | if (n.id == 0 && !n.deleted && n.get("created_by") == null) {
|
---|
84 | n.put("created_by", "JOSM");
|
---|
85 | sendRequest("PUT", "node", n, true);
|
---|
86 | } else if (n.deleted) {
|
---|
87 | sendRequest("DELETE", "node", n, false);
|
---|
88 | } else {
|
---|
89 | sendRequest("PUT", "node", n, true);
|
---|
90 | }
|
---|
91 | processed.add(n);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Upload a segment (without the nodes).
|
---|
96 | */
|
---|
97 | public void visit(Segment ls) {
|
---|
98 | if (ls.id == 0 && !ls.deleted && ls.get("created_by") == null) {
|
---|
99 | ls.put("created_by", "JOSM");
|
---|
100 | sendRequest("PUT", "segment", ls, true);
|
---|
101 | } else if (ls.deleted) {
|
---|
102 | sendRequest("DELETE", "segment", ls, false);
|
---|
103 | } else {
|
---|
104 | sendRequest("PUT", "segment", ls, true);
|
---|
105 | }
|
---|
106 | processed.add(ls);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Upload a whole way with the complete segment id list.
|
---|
111 | */
|
---|
112 | public void visit(Way w) {
|
---|
113 | if (w.id == 0 && !w.deleted && w.get("created_by") == null) {
|
---|
114 | w.put("created_by", "JOSM");
|
---|
115 | sendRequest("PUT", "way", w, true);
|
---|
116 | } else if (w.deleted) {
|
---|
117 | sendRequest("DELETE", "way", w, false);
|
---|
118 | } else {
|
---|
119 | sendRequest("PUT", "way", w, true);
|
---|
120 | }
|
---|
121 | processed.add(w);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Read a long from the input stream and return it.
|
---|
126 | */
|
---|
127 | private long readId(InputStream inputStream) throws IOException {
|
---|
128 | BufferedReader in = new BufferedReader(new InputStreamReader(
|
---|
129 | inputStream));
|
---|
130 | String s = in.readLine();
|
---|
131 | if (s == null)
|
---|
132 | return 0;
|
---|
133 | try {
|
---|
134 | return Long.parseLong(s);
|
---|
135 | } catch (NumberFormatException e) {
|
---|
136 | return 0;
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Send the request. The objects id will be replaced if it was 0 before
|
---|
142 | * (on add requests).
|
---|
143 | *
|
---|
144 | * @param requestMethod The http method used when talking with the server.
|
---|
145 | * @param urlSuffix The suffix to add at the server url.
|
---|
146 | * @param osm The primitive to encode to the server.
|
---|
147 | * @param addBody <code>true</code>, if the whole primitive body should be added.
|
---|
148 | * <code>false</code>, if only the id is encoded.
|
---|
149 | */
|
---|
150 | @SuppressWarnings("unchecked")
|
---|
151 | private void sendRequest(String requestMethod, String urlSuffix,
|
---|
152 | OsmPrimitive osm, boolean addBody) {
|
---|
153 | try {
|
---|
154 | URL url = new URL(Main.pref.get("osm-server.url") + "/0.3/" + urlSuffix + "/" + osm.id);
|
---|
155 | System.out.println("upload to: "+url);
|
---|
156 | activeConnection = (HttpURLConnection) url.openConnection();
|
---|
157 | activeConnection.setConnectTimeout(15000);
|
---|
158 | activeConnection.setRequestMethod(requestMethod);
|
---|
159 | if (addBody)
|
---|
160 | activeConnection.setDoOutput(true);
|
---|
161 | activeConnection.connect();
|
---|
162 |
|
---|
163 | if (addBody) {
|
---|
164 | Writer out = new OutputStreamWriter(activeConnection.getOutputStream());
|
---|
165 | OsmWriter.outputSingle(out, osm, true);
|
---|
166 | out.close();
|
---|
167 | }
|
---|
168 |
|
---|
169 | int retCode = activeConnection.getResponseCode();
|
---|
170 | if (retCode == 200 && osm.id == 0)
|
---|
171 | osm.id = readId(activeConnection.getInputStream());
|
---|
172 | System.out.println("got return: "+retCode+" with id "+osm.id);
|
---|
173 | String retMsg = activeConnection.getResponseMessage();
|
---|
174 | activeConnection.disconnect();
|
---|
175 | if (retCode == 410 && requestMethod.equals("DELETE"))
|
---|
176 | return; // everything fine.. was already deleted.
|
---|
177 | if (retCode != 200) {
|
---|
178 | StringWriter o = new StringWriter();
|
---|
179 | OsmWriter.outputSingle(o, osm, true);
|
---|
180 | System.out.println(o.getBuffer().toString());
|
---|
181 | throw new RuntimeException(retCode+" "+retMsg);
|
---|
182 | }
|
---|
183 | } catch (UnknownHostException e) {
|
---|
184 | throw new RuntimeException("Unknown host: "+e.getMessage(), e);
|
---|
185 | } catch (Exception e) {
|
---|
186 | if (cancel)
|
---|
187 | return; // assume cancel
|
---|
188 | if (e instanceof RuntimeException)
|
---|
189 | throw (RuntimeException)e;
|
---|
190 | throw new RuntimeException(e.getMessage(), e);
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|