Changeset 7663 in josm for trunk/src/org


Ignore:
Timestamp:
2014-10-27T23:53:47+01:00 (5 years ago)
Author:
Don-vip
Message:

fix #10666 - API support for uploading note changes (patch by ToeBee)

Location:
trunk/src/org/openstreetmap/josm/io
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/NoteReader.java

    r7474 r7663  
    22package org.openstreetmap.josm.io;
    33
     4import java.io.ByteArrayInputStream;
    45import java.io.IOException;
    56import java.io.InputStream;
     7import java.nio.charset.StandardCharsets;
    68import java.text.ParseException;
    79import java.text.SimpleDateFormat;
     
    218220
    219221    /**
     222     * Initializes the reader with a string as a source
     223     * @param source UTF-8 string containing Notes XML to parse
     224     * @throws IOException
     225     */
     226    public NoteReader(String source) throws IOException {
     227        this.inputSource = new InputSource(new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)));
     228    }
     229
     230    /**
    220231     * Parses the InputStream given to the constructor and returns
    221232     * the resulting Note objects
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r7656 r7663  
    1515import java.io.StringReader;
    1616import java.io.StringWriter;
     17import java.io.UnsupportedEncodingException;
    1718import java.net.ConnectException;
    1819import java.net.HttpURLConnection;
     
    2021import java.net.SocketTimeoutException;
    2122import java.net.URL;
     23import java.net.URLEncoder;
    2224import java.nio.charset.StandardCharsets;
    2325import java.util.Collection;
    2426import java.util.Collections;
    2527import java.util.HashMap;
     28import java.util.List;
    2629import java.util.Map;
    2730
     
    2932
    3033import org.openstreetmap.josm.Main;
     34import org.openstreetmap.josm.data.coor.LatLon;
     35import org.openstreetmap.josm.data.notes.Note;
    3136import org.openstreetmap.josm.data.osm.Changeset;
    3237import org.openstreetmap.josm.data.osm.IPrimitive;
     
    782787        this.changeset = changeset;
    783788    }
     789
     790    /**
     791     * Create a new note on the server
     792     * @param latlon Location of note
     793     * @param text Comment entered by user to open the note
     794     * @param monitor Progress monitor
     795     * @return Note as it exists on the server after creation (ID assigned)
     796     * @throws OsmTransferException
     797     */
     798    public Note createNote(LatLon latlon, String text, ProgressMonitor monitor) throws OsmTransferException {
     799        initialize(monitor);
     800        String url = new StringBuilder()
     801            .append("notes?lat=")
     802            .append(latlon.lat())
     803            .append("&lon=")
     804            .append(latlon.lon())
     805            .append("&text=")
     806            .append(urlEncode(text)).toString();
     807
     808        String response = sendRequest("POST", url, null, monitor, true, false);
     809        return parseSingleNote(response);
     810    }
     811
     812    /**
     813     * Add a comment to an existing note.
     814     * @param note The note to add a comment to
     815     * @param comment Text of the comment
     816     * @param monitor Progress monitor
     817     * @return Note returned by the API after the comment was added
     818     * @throws OsmTransferException
     819     */
     820    public Note addCommentToNote(Note note, String comment, ProgressMonitor monitor) throws OsmTransferException {
     821        initialize(monitor);
     822        String url = new StringBuilder()
     823            .append("notes/")
     824            .append(note.getId())
     825            .append("/comment?text=")
     826            .append(urlEncode(comment)).toString();
     827
     828        String response = sendRequest("POST", url, null, monitor, true, false);
     829        return parseSingleNote(response);
     830    }
     831
     832    /**
     833     * Close a note
     834     * @param note Note to close. Must currently be open
     835     * @param closeMessage Optional message supplied by the user when closing the note
     836     * @param monitor Progress monitor
     837     * @return Note returned by the API after the close operation
     838     * @throws OsmTransferException
     839     */
     840    public Note closeNote(Note note, String closeMessage, ProgressMonitor monitor) throws OsmTransferException {
     841        initialize(monitor);
     842        String encodedMessage = urlEncode(closeMessage);
     843        StringBuilder urlBuilder = new StringBuilder()
     844            .append("notes/")
     845            .append(note.getId())
     846            .append("/close");
     847        if (encodedMessage != null && !encodedMessage.trim().isEmpty()) {
     848            urlBuilder.append("?text=");
     849            urlBuilder.append(encodedMessage);
     850        }
     851
     852        String response = sendRequest("POST", urlBuilder.toString(), null, monitor, true, false);
     853        return parseSingleNote(response);
     854    }
     855
     856    /**
     857     * Reopen a closed note
     858     * @param note Note to reopen. Must currently be closed
     859     * @param reactivateMessage Optional message supplied by the user when reopening the note
     860     * @param monitor Progress monitor
     861     * @return Note returned by the API after the reopen operation
     862     * @throws OsmTransferException
     863     */
     864    public Note reopenNote(Note note, String reactivateMessage, ProgressMonitor monitor) throws OsmTransferException {
     865        initialize(monitor);
     866        String encodedMessage = urlEncode(reactivateMessage);
     867        StringBuilder urlBuilder = new StringBuilder()
     868            .append("notes/")
     869            .append(note.getId())
     870            .append("/reopen");
     871        if (encodedMessage != null && !encodedMessage.trim().isEmpty()) {
     872            urlBuilder.append("?text=");
     873            urlBuilder.append(encodedMessage);
     874        }
     875
     876        String response = sendRequest("POST", urlBuilder.toString(), null, monitor, true, false);
     877        return parseSingleNote(response);
     878    }
     879
     880    /** Method for parsing API responses for operations on individual notes */
     881    private Note parseSingleNote(String xml) throws OsmTransferException {
     882        try {
     883            List<Note> newNotes = new NoteReader(xml).parse();
     884            if(newNotes.size() == 1) {
     885                return newNotes.get(0);
     886            }
     887            //Shouldn't ever execute. Server will either respond with an error (caught elsewhere) or one note
     888            throw new OsmTransferException(tr("Note upload failed"));
     889        } catch (SAXException|IOException e) {
     890            Main.error(e, true);
     891            throw new OsmTransferException(tr("Error parsing note response from server"), e);
     892        }
     893    }
     894
     895    /** URL encodes a string. Useful for transforming user input into URL query strings*/
     896    private String urlEncode(String string) throws OsmTransferException {
     897        try {
     898            return URLEncoder.encode(string, "UTF-8");
     899        } catch (UnsupportedEncodingException e) {
     900            Main.error(e, true);
     901            throw new OsmTransferException(tr("Error encoding string: {0}", string), e);
     902        }
     903    }
    784904}
Note: See TracChangeset for help on using the changeset viewer.