public class OsmApi extends OsmConnection
Modifier and Type | Class and Description |
---|---|
private class |
OsmApi.CapabilitiesCache |
Modifier and Type | Field and Description |
---|---|
private Capabilities |
capabilities
API capabilities
|
private Changeset |
changeset
Object describing current changeset
|
static String |
DEFAULT_API_URL
Default URL of the standard OSM API.
|
static int |
DEFAULT_MAX_NUM_RETRIES
Maximum number of retries to send a request in case of HTTP 500 errors or timeouts
|
private boolean |
initialized
true if successfully initialized
|
private static Map<String,OsmApi> |
instances |
static int |
MAX_DOWNLOAD_THREADS
Maximum number of concurrent download threads, imposed by
OSM API usage policy.
|
private String |
serverUrl
Server URL
|
private URL |
url |
private String |
version
API version used for server communications
|
activeConnection, cancel, oauthParameters
Modifier | Constructor and Description |
---|---|
protected |
OsmApi(String serverUrl)
Constructs a new
OsmApi for a specific server URL. |
Modifier and Type | Method and Description |
---|---|
Note |
addCommentToNote(Note note,
String comment,
ProgressMonitor monitor)
Add a comment to an existing note.
|
void |
closeChangeset(Changeset changeset,
ProgressMonitor monitor)
Closes a changeset on the server.
|
Note |
closeNote(Note note,
String closeMessage,
ProgressMonitor monitor)
Close a note.
|
Note |
createNote(LatLon latlon,
String text,
ProgressMonitor monitor)
Create a new note on the server.
|
void |
createPrimitive(IPrimitive osm,
ProgressMonitor monitor)
Creates an OSM primitive on the server.
|
void |
deletePrimitive(OsmPrimitive osm,
ProgressMonitor monitor)
Deletes an OSM primitive on the server.
|
protected void |
ensureValidChangeset()
Ensures that the current changeset can be used for uploading data
|
String |
getBaseUrl()
Returns the base URL for API requests, including the negotiated version number.
|
private static String |
getBaseUrl(String serverUrl,
String version) |
Capabilities |
getCapabilities()
Replies the API capabilities.
|
Changeset |
getChangeset()
Replies the changeset data uploads are currently directed to
|
String |
getHost()
Replies the host name of the server URL.
|
protected int |
getMaxRetries()
Replies the max.
|
static OsmApi |
getOsmApi()
Replies the
OsmApi for the URL given by the preference osm-server.url |
static OsmApi |
getOsmApi(String serverUrl)
Replies the
OsmApi for a given server URL |
private static String |
getServerUrlFromPref() |
String |
getVersion()
Replies the OSM protocol version we use to talk to the server.
|
void |
initialize(ProgressMonitor monitor)
Initializes this component by negotiating a protocol version with the server.
|
void |
initialize(ProgressMonitor monitor,
boolean fastFail)
Initializes this component by negotiating a protocol version with the server, with the ability to control the timeout.
|
private void |
initializeCapabilities(String xml) |
static boolean |
isUsingOAuth()
Determines if JOSM is configured to access OSM API via OAuth
|
void |
modifyPrimitive(IPrimitive osm,
ProgressMonitor monitor)
Modifies an OSM primitive on the server.
|
private static StringBuilder |
noteStringBuilder(Note note) |
void |
openChangeset(Changeset changeset,
ProgressMonitor progressMonitor)
Creates a new changeset based on the keys in
changeset . |
private Note |
parseSingleNote(String xml)
Method for parsing API responses for operations on individual notes
|
Note |
reopenNote(Note note,
String reactivateMessage,
ProgressMonitor monitor)
Reopen a closed note
|
protected String |
sendRequest(String requestMethod,
String urlSuffix,
String requestBody,
ProgressMonitor monitor) |
protected String |
sendRequest(String requestMethod,
String urlSuffix,
String requestBody,
ProgressMonitor monitor,
boolean doAuthenticate,
boolean fastFail)
Generic method for sending requests to the OSM API.
|
void |
setChangeset(Changeset changeset)
Sets the changesets to which further data uploads are directed.
|
private void |
sleepAndListen(int retry,
ProgressMonitor monitor) |
private String |
toXml(Changeset s)
Makes an XML string from an OSM primitive.
|
private String |
toXml(IPrimitive o,
boolean addBody)
Makes an XML string from an OSM primitive.
|
void |
updateChangeset(Changeset changeset,
ProgressMonitor monitor)
Updates a changeset with the keys in
changesetUpdate . |
Collection<OsmPrimitive> |
uploadDiff(Collection<? extends OsmPrimitive> list,
ProgressMonitor monitor)
Uploads a list of changes in "diff" form to the server.
|
addAuth, addBasicAuthorizationHeader, addOAuthAuthorizationHeader, cancel, isCanceled
public static final int DEFAULT_MAX_NUM_RETRIES
public static final int MAX_DOWNLOAD_THREADS
public static final String DEFAULT_API_URL
private Capabilities capabilities
private boolean initialized
protected OsmApi(String serverUrl)
OsmApi
for a specific server URL.serverUrl
- the server URL. Must not be nullIllegalArgumentException
- if serverUrl is nullpublic static OsmApi getOsmApi(String serverUrl)
OsmApi
for a given server URLserverUrl
- the server URLIllegalArgumentException
- if serverUrl is nullprivate static String getServerUrlFromPref()
public static OsmApi getOsmApi()
OsmApi
for the URL given by the preference osm-server.url
public String getVersion()
public String getHost()
public void initialize(ProgressMonitor monitor) throws OsmTransferCanceledException, OsmApiInitializationException
monitor
- the progress monitorOsmTransferCanceledException
- If the initialisation has been cancelled by user.OsmApiInitializationException
- If any other exception occurs. Use getCause() to get the original exception.public void initialize(ProgressMonitor monitor, boolean fastFail) throws OsmTransferCanceledException, OsmApiInitializationException
monitor
- the progress monitorfastFail
- true to request quick initialisation with a small timeout (more likely to throw exception)OsmTransferCanceledException
- If the initialisation has been cancelled by user.OsmApiInitializationException
- If any other exception occurs. Use getCause() to get the original exception.private void initializeCapabilities(String xml) throws SAXException, IOException, ParserConfigurationException
private String toXml(IPrimitive o, boolean addBody)
o
- the OSM primitiveaddBody
- true to generate the full XML, false to only generate the encapsulating tagprivate String toXml(Changeset s)
s
- the changesetprivate static String getBaseUrl(String serverUrl, String version)
public String getBaseUrl()
public void createPrimitive(IPrimitive osm, ProgressMonitor monitor) throws OsmTransferException
osm
- the primitivemonitor
- the progress monitorOsmTransferException
- if something goes wrongpublic void modifyPrimitive(IPrimitive osm, ProgressMonitor monitor) throws OsmTransferException
osm
- the primitive. Must not be null.monitor
- the progress monitorOsmTransferException
- if something goes wrongpublic void deletePrimitive(OsmPrimitive osm, ProgressMonitor monitor) throws OsmTransferException
osm
- the primitivemonitor
- the progress monitorOsmTransferException
- if something goes wrongpublic void openChangeset(Changeset changeset, ProgressMonitor progressMonitor) throws OsmTransferException
changeset
. If this
method succeeds, changeset.getId() replies the id the server assigned to the new
changeset
The changeset must not be null, but its key/value-pairs may be empty.changeset
- the changeset toe be created. Must not be null.progressMonitor
- the progress monitorOsmTransferException
- signifying a non-200 return code, or connection errorsIllegalArgumentException
- if changeset is nullpublic void updateChangeset(Changeset changeset, ProgressMonitor monitor) throws OsmTransferException
changesetUpdate
. The changeset must not
be null and id > 0 must be true.changeset
- the changeset to update. Must not be null.monitor
- the progress monitor. If null, uses the NullProgressMonitor.INSTANCE
.OsmTransferException
- if something goes wrong.IllegalArgumentException
- if changeset is nullIllegalArgumentException
- if changeset.getId() <= 0public void closeChangeset(Changeset changeset, ProgressMonitor monitor) throws OsmTransferException
changeset
- the changeset to be closed. Must not be null. changeset.getId() > 0 required.monitor
- the progress monitor. If null, uses NullProgressMonitor.INSTANCE
OsmTransferException
- if something goes wrong.IllegalArgumentException
- if changeset is nullIllegalArgumentException
- if changeset.getId() <= 0public Collection<OsmPrimitive> uploadDiff(Collection<? extends OsmPrimitive> list, ProgressMonitor monitor) throws OsmTransferException
list
- the list of changed OSM Primitivesmonitor
- the progress monitorOsmTransferException
- if something is wrongprivate void sleepAndListen(int retry, ProgressMonitor monitor) throws OsmTransferCanceledException
OsmTransferCanceledException
protected int getMaxRetries()
public static final boolean isUsingOAuth()
true
if JOSM is configured to access OSM API via OAuth, false
otherwiseprotected final String sendRequest(String requestMethod, String urlSuffix, String requestBody, ProgressMonitor monitor) throws OsmTransferException
OsmTransferException
protected final String sendRequest(String requestMethod, String urlSuffix, String requestBody, ProgressMonitor monitor, boolean doAuthenticate, boolean fastFail) throws OsmTransferException
requestMethod
- The http method used when talking with the server.urlSuffix
- The suffix to add at the server url, not including the version number,
but including any object ids (e.g. "/way/1234/history").requestBody
- the body of the HTTP request, if any.monitor
- the progress monitordoAuthenticate
- set to true, if the request sent to the server shall include authentication
credentials;fastFail
- true to request a short timeoutOsmTransferException
- if the HTTP return code was not 200 (and retries have
been exhausted), or rewrapping a Java exception.public Capabilities getCapabilities()
protected void ensureValidChangeset() throws OsmTransferException
OsmTransferException
- if the current changeset can't be used for uploading datapublic Changeset getChangeset()
public void setChangeset(Changeset changeset)
changeset
- the changesetIllegalArgumentException
- if changeset.getId() <= 0IllegalArgumentException
- if !changeset.isOpen()private static StringBuilder noteStringBuilder(Note note)
public Note createNote(LatLon latlon, String text, ProgressMonitor monitor) throws OsmTransferException
latlon
- Location of notetext
- Comment entered by user to open the notemonitor
- Progress monitorOsmTransferException
- if any error occurs during dialog with OSM APIpublic Note addCommentToNote(Note note, String comment, ProgressMonitor monitor) throws OsmTransferException
note
- The note to add a comment tocomment
- Text of the commentmonitor
- Progress monitorOsmTransferException
- if any error occurs during dialog with OSM APIpublic Note closeNote(Note note, String closeMessage, ProgressMonitor monitor) throws OsmTransferException
note
- Note to close. Must currently be opencloseMessage
- Optional message supplied by the user when closing the notemonitor
- Progress monitorOsmTransferException
- if any error occurs during dialog with OSM APIpublic Note reopenNote(Note note, String reactivateMessage, ProgressMonitor monitor) throws OsmTransferException
note
- Note to reopen. Must currently be closedreactivateMessage
- Optional message supplied by the user when reopening the notemonitor
- Progress monitorOsmTransferException
- if any error occurs during dialog with OSM APIprivate Note parseSingleNote(String xml) throws OsmTransferException
xml
- the API response as XML dataOsmTransferException
- if the API response cannot be parsed