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
RevLine 
[33]1package org.openstreetmap.josm.io;
2
[34]3import java.io.BufferedReader;
[33]4import java.io.IOException;
[34]5import java.io.InputStream;
6import java.io.InputStreamReader;
7import java.io.OutputStream;
8import java.net.HttpURLConnection;
9import java.net.URL;
10import java.util.Collection;
[33]11
[34]12import org.jdom.Document;
13import org.jdom.Element;
[33]14import org.jdom.JDOMException;
[34]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;
[33]25
26/**
27 * Class that uploades all changes to the osm server.
28 *
[35]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.
[33]32 *
[35]33 * This class implements visitor and will perform the correct upload action on
34 * the visited element.
[34]35 *
[33]36 * @author imi
37 */
[34]38public class OsmServerWriter extends OsmConnection implements Visitor {
[33]39
40 /**
[35]41 * Send the dataset to the server. Ask the user first and does nothing if he
42 * does not want to send the data.
[33]43 */
[34]44 public void uploadOsm(Collection<OsmPrimitive> list) throws JDOMException {
[33]45 initAuthentication();
[34]46
47 try {
[35]48 for (OsmPrimitive osm : list)
49 osm.visit(this);
[34]50 } catch (RuntimeException e) {
51 throw new JDOMException("An error occoured: ", e);
52 }
[33]53 }
[34]54
55 /**
56 * Upload a single node.
57 */
58 @SuppressWarnings("unchecked")
59 public void visit(Node n) {
[35]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);
[34]64 } else {
[35]65 sendRequest("PUT", "node/" + n.id, n, true);
[34]66 }
67 }
68
69 public void visit(LineSegment ls) {
[35]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 }
[34]77 }
78
79 public void visit(Track t) {
[35]80 // not implemented in server
[34]81 }
82
83 public void visit(Key k) {
[35]84 // not implemented in server
[34]85 }
86
87 /**
88 * Read an long from the input stream and return it.
89 */
90 private long readId(InputStream inputStream) throws IOException {
[35]91 BufferedReader in = new BufferedReader(new InputStreamReader(
92 inputStream));
[34]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 }
[35]102
[34]103 @SuppressWarnings("unchecked")
[35]104 private void sendRequest(String requestMethod, String urlSuffix,
105 OsmPrimitive osm, boolean addBody) {
[34]106 try {
107 URL url = new URL(Main.pref.osmDataServer + "/" + urlSuffix);
[35]108 HttpURLConnection con = (HttpURLConnection) url.openConnection();
[34]109 con.setConnectTimeout(20000);
110 con.setRequestMethod(requestMethod);
[35]111 if (addBody)
112 con.setDoOutput(true);
[34]113 con.connect();
114
[35]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
[34]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 }
[33]137}
Note: See TracBrowser for help on using the repository browser.