Package org.openstreetmap.josm.io
Class OsmApi
- java.lang.Object
-
- org.openstreetmap.josm.io.OsmConnection
-
- org.openstreetmap.josm.io.OsmApi
-
public class OsmApi extends OsmConnection
Class that encapsulates the communications with the OSM API.
All interaction with the server-side OSM API should go through this class.
It is conceivable to extract this into an interface later and create various classes implementing the interface, to be able to talk to various kinds of servers.- Since:
- 1523
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
OsmApi.CapabilitiesCache
static interface
OsmApi.OsmApiInitializationListener
OSM API initialization listener.-
Nested classes/interfaces inherited from class org.openstreetmap.josm.io.OsmConnection
OsmConnection.OAuthAccessTokenFetcher
-
-
Field Summary
Fields Modifier and Type Field Description private Capabilities
capabilities
API capabilitiesprivate Changeset
changeset
Object describing current changesetprivate static java.lang.String
CHANGESET_SLASH
private static java.lang.String
CHANGESET_STR
static int
DEFAULT_MAX_NUM_RETRIES
Maximum number of retries to send a request in case of HTTP 500 errors or timeoutsprivate static java.lang.String
ERROR_MESSAGE
private boolean
initialized
true if successfully initializedprivate static java.util.Map<java.lang.String,OsmApi>
instances
private static ListenerList<OsmApi.OsmApiInitializationListener>
listeners
static int
MAX_DOWNLOAD_THREADS
Maximum number of concurrent download threads, imposed by OSM API usage policy.private java.lang.String
serverUrl
Server URLprivate java.net.URL
url
static BooleanProperty
USE_OAUTH_FOR_ALL_REQUESTS
Defines whether all OSM API requests should be signed with an OAuth token (user-based bandwidth limit instead of IP-based one)private java.lang.String
version
API version used for server communications-
Fields inherited from class org.openstreetmap.josm.io.OsmConnection
activeConnection, cancel, fetcher, oAuth20Parameters
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
OsmApi(java.lang.String serverUrl)
Constructs a newOsmApi
for a specific server URL.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addCommentToChangeset(Changeset changeset, java.lang.String comment, ProgressMonitor monitor)
Adds a comment to the discussion of a closed changeset.Note
addCommentToNote(Note note, java.lang.String comment, ProgressMonitor monitor)
Add a comment to an existing note.static void
addOsmApiInitializationListener(OsmApi.OsmApiInitializationListener listener)
Adds a new OSM API initialization listener.protected static void
cacheInstance(OsmApi api)
void
closeChangeset(Changeset changeset, ProgressMonitor monitor)
Closes a changeset on the server.Note
closeNote(Note note, java.lang.String closeMessage, ProgressMonitor monitor)
Close a note.Note
createNote(LatLon latlon, java.lang.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 datastatic java.lang.String
getAuthMethod()
Returns the authentication method set in the preferencesstatic OAuthVersion
getAuthMethodVersion()
Returns the authentication method set in the preferencesjava.lang.String
getBaseUrl()
Returns the base URL for API requests, including the negotiated version number.private static java.lang.String
getBaseUrl(java.lang.String serverUrl, java.lang.String version)
Capabilities
getCapabilities()
Replies the API capabilities.Changeset
getChangeset()
Replies the changeset data uploads are currently directed tojava.lang.String
getHost()
Replies the host name of the server URL.protected int
getMaxRetries()
Replies the max.static OsmApi
getOsmApi()
Replies theOsmApi
for the URL given by the preferenceosm-server.url
static OsmApi
getOsmApi(java.lang.String serverUrl)
Replies theOsmApi
for a given server URLjava.lang.String
getServerUrl()
Returns the server URLprivate static java.lang.String
getServerUrlFromPref()
java.lang.String
getVersion()
Replies the OSM protocol version we use to talk to the server.private void
individualPrimitiveModification(java.lang.String method, java.lang.String verb, IPrimitive osm, ProgressMonitor monitor, java.util.function.Consumer<java.lang.String> consumer, java.util.function.UnaryOperator<java.lang.String> errHandler)
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(java.lang.String xml)
static boolean
isUsingOAuth()
Determines if JOSM is configured to access OSM API via OAuthstatic boolean
isUsingOAuth(OAuthVersion version)
Determines if JOSM is configured to access OSM API via OAuthstatic boolean
isUsingOAuthAndOAuthSetUp(OsmApi api)
Ensure that OAuth is set upvoid
modifyPrimitive(IPrimitive osm, ProgressMonitor monitor)
Modifies an OSM primitive on the server.private static java.lang.StringBuilder
noteStringBuilder(Note note)
void
openChangeset(Changeset changeset, ProgressMonitor progressMonitor)
Creates a new changeset based on the keys inchangeset
.private static Note
parseSingleNote(java.lang.String xml)
Method for parsing API responses for operations on individual notesstatic void
removeOsmApiInitializationListener(OsmApi.OsmApiInitializationListener listener)
Removes an OSM API initialization listener.Note
reopenNote(Note note, java.lang.String reactivateMessage, ProgressMonitor monitor)
Reopen a closed noteprotected java.lang.String
sendPostRequest(java.lang.String urlSuffix, java.lang.String requestBody, ProgressMonitor monitor)
protected java.lang.String
sendPutRequest(java.lang.String urlSuffix, java.lang.String requestBody, ProgressMonitor monitor)
protected java.lang.String
sendRequest(java.lang.String requestMethod, java.lang.String urlSuffix, java.lang.String requestBody, ProgressMonitor monitor)
protected java.lang.String
sendRequest(java.lang.String requestMethod, java.lang.String urlSuffix, java.lang.String requestBody, ProgressMonitor monitor, boolean doAuthenticate, boolean fastFail)
protected java.lang.String
sendRequest(java.lang.String requestMethod, java.lang.String urlSuffix, java.lang.String requestBody, ProgressMonitor monitor, java.lang.String contentType, 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)
protected java.lang.String
toXml(Changeset s)
Makes an XML string from an OSM primitive.protected java.lang.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 inchangesetUpdate
.java.util.Collection<OsmPrimitive>
uploadDiff(java.util.Collection<? extends OsmPrimitive> list, ProgressMonitor monitor)
Uploads a list of changes in "diff" form to the server.-
Methods inherited from class org.openstreetmap.josm.io.OsmConnection
addAuth, addBasicAuthorizationHeader, addOAuth20AuthorizationHeader, cancel, isCanceled, retrieveBasicAuthorizationLogin, setOAuthAccessTokenFetcher
-
-
-
-
Field Detail
-
CHANGESET_STR
private static final java.lang.String CHANGESET_STR
- See Also:
- Constant Field Values
-
CHANGESET_SLASH
private static final java.lang.String CHANGESET_SLASH
- See Also:
- Constant Field Values
-
ERROR_MESSAGE
private static final java.lang.String ERROR_MESSAGE
-
DEFAULT_MAX_NUM_RETRIES
public static final int DEFAULT_MAX_NUM_RETRIES
Maximum number of retries to send a request in case of HTTP 500 errors or timeouts- See Also:
- Constant Field Values
-
MAX_DOWNLOAD_THREADS
public static final int MAX_DOWNLOAD_THREADS
Maximum number of concurrent download threads, imposed by OSM API usage policy.- Since:
- 5386
- See Also:
- Constant Field Values
-
USE_OAUTH_FOR_ALL_REQUESTS
public static final BooleanProperty USE_OAUTH_FOR_ALL_REQUESTS
Defines whether all OSM API requests should be signed with an OAuth token (user-based bandwidth limit instead of IP-based one)
-
listeners
private static final ListenerList<OsmApi.OsmApiInitializationListener> listeners
-
url
private java.net.URL url
-
serverUrl
private final java.lang.String serverUrl
Server URL
-
version
private java.lang.String version
API version used for server communications
-
capabilities
private Capabilities capabilities
API capabilities
-
initialized
private boolean initialized
true if successfully initialized
-
-
Constructor Detail
-
OsmApi
protected OsmApi(java.lang.String serverUrl)
Constructs a newOsmApi
for a specific server URL.- Parameters:
serverUrl
- the server URL. Must not be null- Throws:
java.lang.IllegalArgumentException
- if serverUrl is null
-
-
Method Detail
-
addOsmApiInitializationListener
public static void addOsmApiInitializationListener(OsmApi.OsmApiInitializationListener listener)
Adds a new OSM API initialization listener.- Parameters:
listener
- OSM API initialization listener to add- Since:
- 12804
-
removeOsmApiInitializationListener
public static void removeOsmApiInitializationListener(OsmApi.OsmApiInitializationListener listener)
Removes an OSM API initialization listener.- Parameters:
listener
- OSM API initialization listener to remove- Since:
- 12804
-
getOsmApi
public static OsmApi getOsmApi(java.lang.String serverUrl)
Replies theOsmApi
for a given server URL- Parameters:
serverUrl
- the server URL- Returns:
- the OsmApi
- Throws:
java.lang.IllegalArgumentException
- if serverUrl is null
-
cacheInstance
protected static void cacheInstance(OsmApi api)
-
getServerUrlFromPref
private static java.lang.String getServerUrlFromPref()
-
getOsmApi
public static OsmApi getOsmApi()
Replies theOsmApi
for the URL given by the preferenceosm-server.url
- Returns:
- the OsmApi
-
getVersion
public java.lang.String getVersion()
Replies the OSM protocol version we use to talk to the server.- Returns:
- protocol version, or null if not yet negotiated.
-
getHost
public java.lang.String getHost()
Replies the host name of the server URL.- Returns:
- the host name of the server URL, or null if the server URL is malformed.
-
initialize
public void initialize(ProgressMonitor monitor) throws OsmTransferCanceledException, OsmApiInitializationException
Initializes this component by negotiating a protocol version with the server.- Parameters:
monitor
- the progress monitor- Throws:
OsmTransferCanceledException
- If the initialisation has been cancelled by user.OsmApiInitializationException
- If any other exception occurs. Use getCause() to get the original exception.
-
initialize
public void initialize(ProgressMonitor monitor, boolean fastFail) throws OsmTransferCanceledException, OsmApiInitializationException
Initializes this component by negotiating a protocol version with the server, with the ability to control the timeout.- Parameters:
monitor
- the progress monitorfastFail
- true to request quick initialisation with a small timeout (more likely to throw exception)- Throws:
OsmTransferCanceledException
- If the initialisation has been cancelled by user.OsmApiInitializationException
- If any other exception occurs. Use getCause() to get the original exception.
-
initializeCapabilities
private void initializeCapabilities(java.lang.String xml) throws org.xml.sax.SAXException, java.io.IOException, javax.xml.parsers.ParserConfigurationException
- Throws:
org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException
-
toXml
protected final java.lang.String toXml(IPrimitive o, boolean addBody)
Makes an XML string from an OSM primitive. Uses the OsmWriter class.- Parameters:
o
- the OSM primitiveaddBody
- true to generate the full XML, false to only generate the encapsulating tag- Returns:
- XML string
-
toXml
protected final java.lang.String toXml(Changeset s)
Makes an XML string from an OSM primitive. Uses the OsmWriter class.- Parameters:
s
- the changeset- Returns:
- XML string
-
getBaseUrl
private static java.lang.String getBaseUrl(java.lang.String serverUrl, java.lang.String version)
-
getBaseUrl
public java.lang.String getBaseUrl()
Returns the base URL for API requests, including the negotiated version number.- Returns:
- base URL string
-
getServerUrl
public java.lang.String getServerUrl()
Returns the server URL- Returns:
- the server URL
- Since:
- 9353
-
individualPrimitiveModification
private void individualPrimitiveModification(java.lang.String method, java.lang.String verb, IPrimitive osm, ProgressMonitor monitor, java.util.function.Consumer<java.lang.String> consumer, java.util.function.UnaryOperator<java.lang.String> errHandler) throws OsmTransferException
- Throws:
OsmTransferException
-
createPrimitive
public void createPrimitive(IPrimitive osm, ProgressMonitor monitor) throws OsmTransferException
Creates an OSM primitive on the server. The OsmPrimitive object passed in is modified by giving it the server-assigned id.- Parameters:
osm
- the primitivemonitor
- the progress monitor- Throws:
OsmTransferException
- if something goes wrong
-
modifyPrimitive
public void modifyPrimitive(IPrimitive osm, ProgressMonitor monitor) throws OsmTransferException
Modifies an OSM primitive on the server.- Parameters:
osm
- the primitive. Must not be null.monitor
- the progress monitor- Throws:
OsmTransferException
- if something goes wrong
-
deletePrimitive
public void deletePrimitive(OsmPrimitive osm, ProgressMonitor monitor) throws OsmTransferException
Deletes an OSM primitive on the server.- Parameters:
osm
- the primitivemonitor
- the progress monitor- Throws:
OsmTransferException
- if something goes wrong
-
openChangeset
public void openChangeset(Changeset changeset, ProgressMonitor progressMonitor) throws OsmTransferException
Creates a new changeset based on the keys inchangeset
. If this method succeeds, changeset.getId() replies the id the server assigned to the new changesetThe changeset must not be null, but its key/value-pairs may be empty.
- Parameters:
changeset
- the changeset toe be created. Must not be null.progressMonitor
- the progress monitor- Throws:
OsmTransferException
- signifying a non-200 return code, or connection errorsjava.lang.IllegalArgumentException
- if changeset is null
-
updateChangeset
public void updateChangeset(Changeset changeset, ProgressMonitor monitor) throws OsmTransferException
Updates a changeset with the keys inchangesetUpdate
. The changeset must not be null and id > 0 must be true.- Parameters:
changeset
- the changeset to update. Must not be null.monitor
- the progress monitor. If null, uses theNullProgressMonitor.INSTANCE
.- Throws:
OsmTransferException
- if something goes wrong.java.lang.IllegalArgumentException
- if changeset is nulljava.lang.IllegalArgumentException
- if changeset.getId() <= 0
-
closeChangeset
public void closeChangeset(Changeset changeset, ProgressMonitor monitor) throws OsmTransferException
Closes a changeset on the server. Sets changeset.setOpen(false) if this operation succeeds.- Parameters:
changeset
- the changeset to be closed. Must not be null. changeset.getId() > 0 required.monitor
- the progress monitor. If null, usesNullProgressMonitor.INSTANCE
- Throws:
OsmTransferException
- if something goes wrong.java.lang.IllegalArgumentException
- if changeset is nulljava.lang.IllegalArgumentException
- if changeset.getId() <= 0
-
addCommentToChangeset
public void addCommentToChangeset(Changeset changeset, java.lang.String comment, ProgressMonitor monitor) throws OsmTransferException
Adds a comment to the discussion of a closed changeset.- Parameters:
changeset
- the changeset where to add a comment. Must be closed. changeset.getId() > 0 required.comment
- Text of the commentmonitor
- the progress monitor. If null, usesNullProgressMonitor.INSTANCE
- Throws:
OsmTransferException
- if something goes wrong.- Since:
- 17500
-
uploadDiff
public java.util.Collection<OsmPrimitive> uploadDiff(java.util.Collection<? extends OsmPrimitive> list, ProgressMonitor monitor) throws OsmTransferException
Uploads a list of changes in "diff" form to the server.- Parameters:
list
- the list of changed OSM Primitivesmonitor
- the progress monitor- Returns:
- list of processed primitives
- Throws:
OsmTransferException
- if something is wrong
-
sleepAndListen
private void sleepAndListen(int retry, ProgressMonitor monitor) throws OsmTransferCanceledException
- Throws:
OsmTransferCanceledException
-
getMaxRetries
protected int getMaxRetries()
Replies the max. number of retries in case of 5XX errors on the server- Returns:
- the max number of retries
-
isUsingOAuth
public static boolean isUsingOAuth()
Determines if JOSM is configured to access OSM API via OAuth- Returns:
true
if JOSM is configured to access OSM API via OAuth,false
otherwise- Since:
- 6349
-
isUsingOAuth
public static boolean isUsingOAuth(OAuthVersion version)
Determines if JOSM is configured to access OSM API via OAuth- Parameters:
version
- The OAuth version- Returns:
true
if JOSM is configured to access OSM API via OAuth,false
otherwise- Since:
- 18650
-
isUsingOAuthAndOAuthSetUp
public static boolean isUsingOAuthAndOAuthSetUp(OsmApi api)
Ensure that OAuth is set up- Parameters:
api
- The api for which we need OAuth keys- Returns:
true
if we are using OAuth and there are keys for the specified API
-
getAuthMethod
public static java.lang.String getAuthMethod()
Returns the authentication method set in the preferences- Returns:
- the authentication method
-
getAuthMethodVersion
@Nullable public static OAuthVersion getAuthMethodVersion()
Returns the authentication method set in the preferences- Returns:
- the authentication method
- Since:
- 18991
-
sendPostRequest
protected final java.lang.String sendPostRequest(java.lang.String urlSuffix, java.lang.String requestBody, ProgressMonitor monitor) throws OsmTransferException
- Throws:
OsmTransferException
-
sendPutRequest
protected final java.lang.String sendPutRequest(java.lang.String urlSuffix, java.lang.String requestBody, ProgressMonitor monitor) throws OsmTransferException
- Throws:
OsmTransferException
-
sendRequest
protected final java.lang.String sendRequest(java.lang.String requestMethod, java.lang.String urlSuffix, java.lang.String requestBody, ProgressMonitor monitor) throws OsmTransferException
- Throws:
OsmTransferException
-
sendRequest
protected final java.lang.String sendRequest(java.lang.String requestMethod, java.lang.String urlSuffix, java.lang.String requestBody, ProgressMonitor monitor, boolean doAuthenticate, boolean fastFail) throws OsmTransferException
- Throws:
OsmTransferException
-
sendRequest
protected final java.lang.String sendRequest(java.lang.String requestMethod, java.lang.String urlSuffix, java.lang.String requestBody, ProgressMonitor monitor, java.lang.String contentType, boolean doAuthenticate, boolean fastFail) throws OsmTransferException
Generic method for sending requests to the OSM API.This method will automatically re-try any requests that are answered with a 5xx error code, or that resulted in a timeout exception from the TCP layer.
- Parameters:
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 monitorcontentType
- Content-Type to set for PUT/POST/DELETE requests. Can be set tonull
, in that case it meanstext/xml
doAuthenticate
- set to true, if the request sent to the server shall include authentication credentials;fastFail
- true to request a short timeout- Returns:
- the body of the HTTP response, if and only if the response code was "200 OK".
- Throws:
OsmTransferException
- if the HTTP return code was not 200 (and retries have been exhausted), or rewrapping a Java exception.
-
getCapabilities
public Capabilities getCapabilities()
Replies the API capabilities.- Returns:
- the API capabilities, or null, if the API is not initialized yet
-
ensureValidChangeset
protected void ensureValidChangeset() throws OsmTransferException
Ensures that the current changeset can be used for uploading data- Throws:
OsmTransferException
- if the current changeset can't be used for uploading data
-
getChangeset
public Changeset getChangeset()
Replies the changeset data uploads are currently directed to- Returns:
- the changeset data uploads are currently directed to
-
setChangeset
public void setChangeset(Changeset changeset)
Sets the changesets to which further data uploads are directed. The changeset can be null. If it isn't null it must have been created, i.e. id > 0 is required. Furthermore, it must be open.- Parameters:
changeset
- the changeset- Throws:
java.lang.IllegalArgumentException
- if changeset.getId() <= 0java.lang.IllegalArgumentException
- if !changeset.isOpen()
-
noteStringBuilder
private static java.lang.StringBuilder noteStringBuilder(Note note)
-
createNote
public Note createNote(LatLon latlon, java.lang.String text, ProgressMonitor monitor) throws OsmTransferException
Create a new note on the server.- Parameters:
latlon
- Location of notetext
- Comment entered by user to open the notemonitor
- Progress monitor- Returns:
- Note as it exists on the server after creation (ID assigned)
- Throws:
OsmTransferException
- if any error occurs during dialog with OSM API
-
addCommentToNote
public Note addCommentToNote(Note note, java.lang.String comment, ProgressMonitor monitor) throws OsmTransferException
Add a comment to an existing note.- Parameters:
note
- The note to add a comment tocomment
- Text of the commentmonitor
- Progress monitor- Returns:
- Note returned by the API after the comment was added
- Throws:
OsmTransferException
- if any error occurs during dialog with OSM API
-
closeNote
public Note closeNote(Note note, java.lang.String closeMessage, ProgressMonitor monitor) throws OsmTransferException
Close a note.- Parameters:
note
- Note to close. Must currently be opencloseMessage
- Optional message supplied by the user when closing the notemonitor
- Progress monitor- Returns:
- Note returned by the API after the close operation
- Throws:
OsmTransferException
- if any error occurs during dialog with OSM API
-
reopenNote
public Note reopenNote(Note note, java.lang.String reactivateMessage, ProgressMonitor monitor) throws OsmTransferException
Reopen a closed note- Parameters:
note
- Note to reopen. Must currently be closedreactivateMessage
- Optional message supplied by the user when reopening the notemonitor
- Progress monitor- Returns:
- Note returned by the API after the reopen operation
- Throws:
OsmTransferException
- if any error occurs during dialog with OSM API
-
parseSingleNote
private static Note parseSingleNote(java.lang.String xml) throws OsmTransferException
Method for parsing API responses for operations on individual notes- Parameters:
xml
- the API response as XML data- Returns:
- the resulting Note
- Throws:
OsmTransferException
- if the API response cannot be parsed
-
-