source: josm/src/org/openstreetmap/josm/io/OsmServerWriter.java@ 66

Last change on this file since 66 was 66, checked in by imi, 18 years ago

fixed "segment completly out of map" - problem

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