use of org.apache.directory.fortress.core.RestException in project directory-fortress-core by apache.
the class RestUtils method post.
/**
* Perform an HTTP Post REST operation.
*
* @param userId
* @param password
* @param szInput
* @param function
* @return String containing response
* @throws RestException
*/
public String post(String userId, String password, String szInput, String function) throws RestException {
LOG.debug("post uri=[{}], function=[{}], request=[{}]", uri, function, szInput);
String szResponse = null;
HttpPost post = new HttpPost(uri + function);
post.addHeader("Accept", "text/xml");
setMethodHeaders(post);
try {
HttpEntity entity = new StringEntity(szInput, ContentType.TEXT_XML);
post.setEntity(entity);
org.apache.http.client.HttpClient httpclient = HttpClientBuilder.create().useSystemProperties().setDefaultCredentialsProvider(getCredentialProvider(userId, password)).build();
HttpResponse response = httpclient.execute(post);
String error;
switch(response.getStatusLine().getStatusCode()) {
case HTTP_OK:
szResponse = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
LOG.debug("post uri=[{}], function=[{}], response=[{}]", uri, function, szResponse);
break;
case HTTP_401_UNAUTHORIZED:
error = generateErrorMessage(uri, function, "401 function unauthorized on host");
LOG.error(error);
throw new RestException(GlobalErrIds.REST_UNAUTHORIZED_ERR, error);
case HTTP_403_FORBIDDEN:
error = generateErrorMessage(uri, function, "403 function forbidden on host");
LOG.error(error);
throw new RestException(GlobalErrIds.REST_FORBIDDEN_ERR, error);
case HTTP_404_NOT_FOUND:
error = generateErrorMessage(uri, function, "404 not found from host");
LOG.error(error);
throw new RestException(GlobalErrIds.REST_NOT_FOUND_ERR, error);
default:
error = generateErrorMessage(uri, function, "error received from host: " + response.getStatusLine().getStatusCode());
LOG.error(error);
throw new RestException(GlobalErrIds.REST_UNKNOWN_ERR, error);
}
} catch (IOException ioe) {
String error = generateErrorMessage(uri, function, "caught IOException=" + ioe.getMessage());
LOG.error(error, ioe);
throw new RestException(GlobalErrIds.REST_IO_ERR, error, ioe);
} catch (WebApplicationException we) {
String error = generateErrorMessage(uri, function, "caught WebApplicationException=" + we.getMessage());
LOG.error(error, we);
throw new RestException(GlobalErrIds.REST_WEB_ERR, error, we);
} catch (java.lang.NoSuchMethodError e) {
String error = generateErrorMessage(uri, function, "caught Exception = " + e.getMessage());
LOG.error(error, e);
throw new RestException(GlobalErrIds.REST_UNKNOWN_ERR, error);
} finally {
// Release current connection to the connection pool.
post.releaseConnection();
}
return szResponse;
}
use of org.apache.directory.fortress.core.RestException in project directory-fortress-core by apache.
the class RestUtils method unmarshall.
/**
* Unmarshall the XML response into its associated Java objects.
*
* @param szResponse
* @return FortResponse
* @throws RestException
*/
public static FortResponse unmarshall(String szResponse) throws RestException {
FortResponse response;
try {
// Create a JAXB context passing in the class of the object we want to marshal/unmarshal
final JAXBContext context = cachedJaxbContext.getJaxbContext(FortResponse.class);
// Create the unmarshaller, that will transform the XML back into an object
final Unmarshaller unmarshaller = context.createUnmarshaller();
response = (FortResponse) unmarshaller.unmarshal(new StringReader(szResponse));
} catch (JAXBException je) {
String error = "unmarshall caught JAXBException=" + je;
throw new RestException(GlobalErrIds.REST_UNMARSHALL_ERR, error, je);
}
return response;
}
use of org.apache.directory.fortress.core.RestException in project directory-fortress-core by apache.
the class RestUtils method marshal.
/**
* Marshall the request into an XML String.
*
* @param request
* @return String containing xml request
* @throws RestException
*/
public static String marshal(FortRequest request) throws RestException {
String szRetValue;
try {
// Create a JAXB context passing in the class of the object we want to marshal/unmarshal
final JAXBContext context = cachedJaxbContext.getJaxbContext(FortRequest.class);
// =============================================================================================================
// Marshalling OBJECT to XML
// =============================================================================================================
// Create the marshaller, that will transform the object into XML
final Marshaller marshaller = context.createMarshaller();
// Create a stringWriter to hold the XML
final StringWriter stringWriter = new StringWriter();
// Marshal the javaObject and write the XML to the stringWriter
marshaller.marshal(request, stringWriter);
szRetValue = stringWriter.toString();
} catch (JAXBException je) {
String error = "marshal caught JAXBException=" + je;
throw new RestException(GlobalErrIds.REST_MARSHALL_ERR, error, je);
}
return szRetValue;
}
use of org.apache.directory.fortress.core.RestException in project directory-fortress-core by apache.
the class RestUtils method handleHttpMethod.
/* private static String base64Encode( String value )
{
return Base64Utility.encode( value.getBytes() );
}*/
/**
* Process the HTTP method request.
*
* @param httpGetRequest
* @return String containing response
* @throws Exception
*/
private static String handleHttpMethod(HttpRequestBase httpGetRequest, org.apache.http.client.HttpClient client) throws RestException {
String szResponse = null;
try {
HttpResponse response = client.execute(httpGetRequest);
LOG.debug("handleHttpMethod Response status : {}", response.getStatusLine().getStatusCode());
Response.Status status = Response.Status.fromStatusCode(response.getStatusLine().getStatusCode());
if (status == Response.Status.OK) {
szResponse = IOUtils.toString(response.getEntity().getContent());
LOG.debug(szResponse);
} else if (status == Response.Status.FORBIDDEN) {
LOG.debug("handleHttpMethod Authorization failure");
} else if (status == Response.Status.UNAUTHORIZED) {
LOG.debug("handleHttpMethod Authentication failure");
} else {
LOG.debug("handleHttpMethod Unknown error");
}
} catch (IOException ioe) {
String error = "handleHttpMethod caught IOException=" + ioe;
LOG.error(error);
throw new RestException(GlobalErrIds.REST_IO_ERR, error, ioe);
} finally {
// Release current connection to the connection pool.
httpGetRequest.releaseConnection();
}
return szResponse;
}
Aggregations