Ticket #10666: note_api.patch

File note_api.patch, 7.1 KB (added by ToeBee, 7 years ago)
  • src/org/openstreetmap/josm/io/NoteReader.java

     
    11// License: GPL. For details, see LICENSE file.
    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;
    810import java.util.ArrayList;
     
    217219    }
    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
    222233     * @return List of Notes parsed from the input data
  • src/org/openstreetmap/josm/io/OsmApi.java

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