Search in sources :

Example 1 with RestException

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) WebApplicationException(javax.ws.rs.WebApplicationException) RestException(org.apache.directory.fortress.core.RestException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity)

Example 2 with RestException

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;
}
Also used : JAXBException(javax.xml.bind.JAXBException) FortResponse(org.apache.directory.fortress.core.model.FortResponse) StringReader(java.io.StringReader) RestException(org.apache.directory.fortress.core.RestException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 3 with RestException

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;
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) JAXBException(javax.xml.bind.JAXBException) RestException(org.apache.directory.fortress.core.RestException) JAXBContext(javax.xml.bind.JAXBContext)

Example 4 with RestException

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;
}
Also used : FortResponse(org.apache.directory.fortress.core.model.FortResponse) Response(javax.ws.rs.core.Response) HttpResponse(org.apache.http.HttpResponse) RestException(org.apache.directory.fortress.core.RestException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException)

Aggregations

RestException (org.apache.directory.fortress.core.RestException)4 IOException (java.io.IOException)2 JAXBContext (javax.xml.bind.JAXBContext)2 JAXBException (javax.xml.bind.JAXBException)2 FortResponse (org.apache.directory.fortress.core.model.FortResponse)2 HttpResponse (org.apache.http.HttpResponse)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Response (javax.ws.rs.core.Response)1 Marshaller (javax.xml.bind.Marshaller)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 HttpEntity (org.apache.http.HttpEntity)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1