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

Last change on this file since 43 was 43, checked in by imi, 18 years ago
  • removed UTM (too complex)
  • added please wait dialog for down-/uploading
  • added created_by=JOSM to every new element
File size: 4.3 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.OutputStream;
8import java.net.HttpURLConnection;
9import java.net.URL;
10import java.net.UnknownHostException;
11import java.util.Collection;
12import java.util.HashMap;
13
14import org.jdom.Document;
15import org.jdom.Element;
16import org.jdom.JDOMException;
17import org.jdom.output.Format;
18import org.jdom.output.XMLOutputter;
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.osm.Key;
21import org.openstreetmap.josm.data.osm.LineSegment;
22import org.openstreetmap.josm.data.osm.Node;
23import org.openstreetmap.josm.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.data.osm.Track;
25import org.openstreetmap.josm.data.osm.visitor.OsmXmlVisitor;
26import org.openstreetmap.josm.data.osm.visitor.Visitor;
27
28/**
29 * Class that uploades all changes to the osm server.
30 *
31 * This is done like this: - All objects with id = 0 are uploaded as new, except
32 * those in deleted, which are ignored - All objects in deleted list are
33 * deleted. - All remaining objects with modified flag set are updated.
34 *
35 * This class implements visitor and will perform the correct upload action on
36 * the visited element.
37 *
38 * @author imi
39 */
40public class OsmServerWriter extends OsmConnection implements Visitor {
41
42 /**
43 * Send the dataset to the server. Ask the user first and does nothing if he
44 * does not want to send the data.
45 */
46 public void uploadOsm(Collection<OsmPrimitive> list) throws JDOMException {
47 initAuthentication();
48
49 try {
50 for (OsmPrimitive osm : list)
51 osm.visit(this);
52 } catch (RuntimeException e) {
53 throw new JDOMException("An error occoured: ", e);
54 }
55 }
56
57 /**
58 * Upload a single node.
59 */
60 public void visit(Node n) {
61 if (n.id == 0 && !n.isDeleted()) {
62 setCredits(n);
63 sendRequest("PUT", "newnode", n, true);
64 } else if (n.isDeleted()) {
65 sendRequest("DELETE", "node/" + n.id, n, false);
66 } else {
67 sendRequest("PUT", "node/" + n.id, n, true);
68 }
69 }
70
71 /**
72 * Upload a line segment (without the nodes).
73 */
74 public void visit(LineSegment ls) {
75 if (ls.id == 0 && !ls.isDeleted()) {
76 setCredits(ls);
77 sendRequest("PUT", "newsegment", ls, true);
78 } else if (ls.isDeleted()) {
79 sendRequest("DELETE", "segment/" + ls.id, ls, false);
80 } else {
81 sendRequest("PUT", "segment/" + ls.id, ls, true);
82 }
83 }
84
85 /**
86 * Add the created_by - property to indicate that JOSM was the
87 * creating application.
88 * @param osm The primitive to add the credits to
89 */
90 private void setCredits(OsmPrimitive osm) {
91 if (osm.keys == null)
92 osm.keys = new HashMap<Key, String>();
93 osm.keys.put(Key.get("created_by"), "JOSM");
94 }
95
96 public void visit(Track t) {
97 // not implemented in server
98 }
99
100 public void visit(Key k) {
101 // not implemented in server
102 }
103
104 /**
105 * Read an long from the input stream and return it.
106 */
107 private long readId(InputStream inputStream) throws IOException {
108 BufferedReader in = new BufferedReader(new InputStreamReader(
109 inputStream));
110 String s = in.readLine();
111 if (s == null)
112 return 0;
113 try {
114 return Long.parseLong(s);
115 } catch (NumberFormatException e) {
116 return 0;
117 }
118 }
119
120 @SuppressWarnings("unchecked")
121 private void sendRequest(String requestMethod, String urlSuffix,
122 OsmPrimitive osm, boolean addBody) {
123 try {
124 URL url = new URL(Main.pref.osmDataServer + "/" + urlSuffix);
125 HttpURLConnection con = (HttpURLConnection) url.openConnection();
126 con.setConnectTimeout(20000);
127 con.setRequestMethod(requestMethod);
128 if (addBody)
129 con.setDoOutput(true);
130 con.connect();
131
132 if (addBody) {
133 OsmXmlVisitor visitor = new OsmXmlVisitor(false);
134 osm.visit(visitor);
135 Element root = new Element("osm");
136 root.setAttribute("version", "0.2");
137 root.getChildren().add(visitor.element);
138 XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
139 OutputStream out = con.getOutputStream();
140 Document doc = new Document(root);
141 xmlOut.output(doc, out);
142 out.close();
143 }
144
145 int retCode = con.getResponseCode();
146 if (retCode == 200 && osm.id == 0)
147 osm.id = readId(con.getInputStream());
148 con.disconnect();
149 } catch (UnknownHostException e) {
150 throw new RuntimeException("Unknown host: "+e.getMessage(), e);
151 } catch (Exception e) {
152 throw new RuntimeException(e.getMessage(), e);
153 }
154 }
155}
Note: See TracBrowser for help on using the repository browser.