[33] | 1 | package org.openstreetmap.josm.io;
|
---|
| 2 |
|
---|
[34] | 3 | import java.io.BufferedReader;
|
---|
[33] | 4 | import java.io.IOException;
|
---|
[34] | 5 | import java.io.InputStream;
|
---|
| 6 | import java.io.InputStreamReader;
|
---|
| 7 | import java.io.OutputStream;
|
---|
| 8 | import java.net.HttpURLConnection;
|
---|
| 9 | import java.net.URL;
|
---|
| 10 | import java.util.Collection;
|
---|
[33] | 11 |
|
---|
[34] | 12 | import org.jdom.Document;
|
---|
| 13 | import org.jdom.Element;
|
---|
[33] | 14 | import org.jdom.JDOMException;
|
---|
[34] | 15 | import org.jdom.output.Format;
|
---|
| 16 | import org.jdom.output.XMLOutputter;
|
---|
| 17 | import org.openstreetmap.josm.Main;
|
---|
| 18 | import org.openstreetmap.josm.data.osm.Key;
|
---|
| 19 | import org.openstreetmap.josm.data.osm.LineSegment;
|
---|
| 20 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 22 | import org.openstreetmap.josm.data.osm.Track;
|
---|
| 23 | import org.openstreetmap.josm.data.osm.visitor.OsmXmlVisitor;
|
---|
| 24 | import 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] | 38 | public 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 | }
|
---|