.. _java_xml_services:
Java development with XML services
==================================
In this chapter are shown some examples to access GeoNetwork XML
services in Java. Apache http commons library is used to send the requests
and retrieve the results.
Retrieve groups list
--------------------
This example shows a simple request, without requiring authentication, to retrieve the GeoNetwork groups.
Source
``````
::
package org.geonetwork.xmlservices.client;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.jdom.Document;
import org.jdom.Element;
public class GetGroupsClient {
public static void main(String args[]) {
**// Create request xml**
Element request = new Element("request");
**// Create PostMethod specifying service url**
String serviceUrl = "http://localhost:8080/geonetwork/srv/en/xml.group.list";
PostMethod post = new PostMethod(serviceUrl);
try {
String postData = Xml.getString(new Document(request));
**// Set post data, mime-type and encoding**
post.setRequestEntity(new StringRequestEntity(postData, "application/xml", "UTF8"));
**// Send request**
HttpClient httpclient = new HttpClient();
int result = httpclient.executeMethod(post);
**// Display status code**
System.out.println("Response status code: " + result);
**// Display response**
System.out.println("Response body: ");
System.out.println(post.getResponseBodyAsString());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
**// Release current connection to the connection pool
// once you are done**
post.releaseConnection();
}
}
}
Output
``````
::
Response status code: 200
Response body:
2sampleDemo groupgroup@mail.net
Create a new user (exception management)
----------------------------------------
This example show a request to create a new user, that requires
authentication to complete succesfully. The request is executed without
authentication to capture the exception returned by GeoNetwork.
Source
``````
::
package org.geonetwork.xmlservices.client;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.jdom.Document;
import org.jdom.Element;
public class CreateUserClient {
public static void main(String args[]) {
**// Create request** xml
Element request = new Element("request")
.addContent(new Element("operation").setText("newuser"))
.addContent(new Element("username").setText("samantha"))
.addContent(new Element("password").setText("editor2"))
.addContent(new Element("profile").setText("Editor"))
.addContent(new Element("name").setText("Samantha"))
.addContent(new Element("city").setText("Amsterdam"))
.addContent(new Element("country").setText("Netherlands"))
.addContent(new Element("email").setText("samantha@mail.net"));
**// Create PostMethod specifying service url**
String serviceUrl = "http://localhost:8080/geonetwork/srv/en/user.update";
PostMethod post = new PostMethod(serviceUrl);
try {
String postData = Xml.getString(new Document(request));
**// Set post data, mime-type and encoding**
post.setRequestEntity(new StringRequestEntity(postData, "application/xml", "UTF8"));
**// Send request**
HttpClient httpclient = new HttpClient();
int result = httpclient.executeMethod(post);
**// Display status code**
System.out.println("Response status code: " + result);
**// Display response**
System.out.println("Response body: ");
String responseBody = post.getResponseBodyAsString();
System.out.println(responseBody);
if (result != HttpStatus.SC_OK) {
**// Process exception**
Element response = Xml.loadString(responseBody, false);
System.out.println("Error code: " +
response.getAttribute("id").getValue());
System.out.println("Error message: " +
response.getChildText("message"));
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// Release current connection to the connection pool
// once you are done
post.releaseConnection();
}
}
}
Output
``````
::
Response status code: 401
Response body:
Service not allowedServiceNotAllowedExenuser.update
Error code: service-not-allowed
Error message: Service not allowed
Create a new user (sending credentials)
---------------------------------------
This example show a request to create a new user, that requires
authentication to complete succesfully.
In this example **httpClient** it's used first to
send a login request to GeoNetwork, getting with
**JSESSIONID** cookie. Nexts requests send to
GeoNetwork using **httpClient** send the
**JSESSIONID** cookie, and are managed as authenticated
requests.
Source
``````
::
package org.geonetwork.xmlservices.client;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.jdom.Document;
import org.jdom.Element;
public class CreateUserClientAuth {
private HttpClient httpclient;
CreateUserClientAuth() {
httpclient = new HttpClient();
}
**/\**
* Authenticates the user in GeoNetwork and send a request
* that needs authentication to create a new user
*
\*/**
public void sendRequest() {
**// Authenticate user**
if (!login()) System.exit(-1);
**// Create request XML**
Element request = new Element("request")
.addContent(new Element("operation").setText("newuser"))
.addContent(new Element("username").setText("samantha"))
.addContent(new Element("password").setText("editor2"))
.addContent(new Element("profile").setText("Editor"))
.addContent(new Element("name").setText("Samantha"))
.addContent(new Element("city").setText("Amsterdam"))
.addContent(new Element("country").setText("Netherlands"))
.addContent(new Element("email").setText("samantha@mail.net"));
**// Create PostMethod specifying service url**
String serviceUrl = "http://localhost:8080/geonetwork/srv/en/user.update";
PostMethod post = new PostMethod(serviceUrl);
try {
String postData = Xml.getString(new Document(request));
**// Set post data, mime-type and encoding**
post.setRequestEntity(new StringRequestEntity(postData, "application/xml", "UTF8"));
**// Send request**
**(httpClient has been set in
// login request with JSESSIONID cookie)**
int result = httpclient.executeMethod(post);
**// Display status code**
System.out.println("Create user response status code: " + result);
if (result != HttpStatus.SC_OK) {
**// Process exception**
String responseBody = post.getResponseBodyAsString();
Element response = Xml.loadString(responseBody, false);
System.out.println("Error code: " +
response.getAttribute("id").getValue());
System.out.println("Error message: " +
response.getChildText("message"));
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
**// Release current connection to the connection pool
// once you are done**
post.releaseConnection();
}
}
**/\**
* Logins a user in GeoNetwork
*
* After login **httpClient** gets with JSSESIONID cookie. Using it
* for nexts requests, these are managed as "authenticated requests"
*
* @return True if login it's ok, false otherwise
\*/**
private boolean login() {
**// Create request XML**
Element request = new Element("request")
.addContent(new Element("username").setText("admin"))
.addContent(new Element("password").setText("admin"));
**// Create PostMethod specifying login service url**
String loginUrl =
"http://localhost:8080/geonetwork/srv/en/xml.user.login";
PostMethod post = new PostMethod(loginUrl);
try {
String postData = Xml.getString(new Document(request));
**// Set post data, mime-type and encoding**
post.setRequestEntity(new StringRequestEntity(postData,
"application/xml", "UTF8"));
**// Send login request**
int result = httpclient.executeMethod(post);
**// Display status code and authentication session cookie**
System.out.println("Login response status code: " + result);
System.out.println("Authentication session cookie: " +
httpclient.getState().getCookies()[0]);
return (result == HttpStatus.SC_OK);
} catch (Exception ex) {
ex.printStackTrace();
return false;
} finally {
// Release current connection to the connection pool
// once you are done
post.releaseConnection();
}
}
public static void main(String args[]) {
CreateUserClientAuth request = new CreateUserClientAuth();
request.sendRequest();
}
}
Output
``````
::
Login response status code: 200
Authentication session cookie: JSESSIONID=ozj8iyva0agv
Create user response status code: 200
Trying to run again the program, as the user it's just created we get an exception:
::
Login response status code: 200
Authentication session cookie: JSESSIONID=1q09kwg0r6fqe
Create user response status code: 500
Error response::
ERROR: duplicate key violates unique constraint "users_username_key"PSQLExceptionenuser.update
Error code: error
Error message: ERROR: duplicate key violates unique constraint "users_username_key"