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

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