Example usage for org.apache.http.client.methods HttpUriRequest getURI

List of usage examples for org.apache.http.client.methods HttpUriRequest getURI

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest getURI.

Prototype

URI getURI();

Source Link

Document

Returns the URI this request uses, such as <code>http://example.org/path/to/file</code>.

Usage

From source file:org.flowable.admin.service.engine.FlowableClientService.java

public ResponseInfo execute(HttpUriRequest request, String userName, String password,
        int... expectedStatusCodes) {

    FlowableServiceException exception = null;
    CloseableHttpClient client = getHttpClient(userName, password);
    try {/*from  www  .jav  a  2s .  c o  m*/
        CloseableHttpResponse response = client.execute(request);

        try {
            JsonNode bodyNode = readJsonContent(response.getEntity().getContent());

            int statusCode = -1;
            if (response.getStatusLine() != null) {
                statusCode = response.getStatusLine().getStatusCode();
            }
            boolean success = Arrays.asList(expectedStatusCodes).contains(statusCode);

            if (success) {
                return new ResponseInfo(statusCode, bodyNode);

            } else {
                exception = new FlowableServiceException(
                        extractError(readJsonContent(response.getEntity().getContent()),
                                "An error occurred while calling Flowable: " + response.getStatusLine()));
            }
        } catch (Exception e) {
            log.warn("Error consuming response from uri " + request.getURI(), e);
            exception = wrapException(e, request);
        } finally {
            response.close();
        }

    } catch (Exception e) {
        log.error("Error executing request to uri " + request.getURI(), e);
        exception = wrapException(e, request);
    } finally {
        try {
            client.close();
        } catch (Exception e) {
            log.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

    return null;
}

From source file:org.flowable.admin.service.engine.FlowableClientService.java

public void execute(HttpUriRequest request, HttpServletResponse httpResponse, String userName,
        String password) {/*ww  w  .j a  v  a  2  s .  c o  m*/

    FlowableServiceException exception = null;
    CloseableHttpClient client = getHttpClient(userName, password);
    try {
        CloseableHttpResponse response = client.execute(request);

        try {
            if (response.getStatusLine() != null
                    && response.getStatusLine().getStatusCode() != HttpStatus.SC_UNAUTHORIZED) {
                httpResponse.setStatus(response.getStatusLine().getStatusCode());
                if (response.getEntity() != null && response.getEntity().getContentType() != null) {
                    httpResponse.setContentType(response.getEntity().getContentType().getValue());
                    response.getEntity().writeTo(httpResponse.getOutputStream());
                }
            } else {
                exception = new FlowableServiceException(
                        extractError(readJsonContent(response.getEntity().getContent()),
                                "An error occurred while calling Flowable: " + response.getStatusLine()));
            }
        } catch (Exception e) {
            log.warn("Error consuming response from uri " + request.getURI(), e);
            exception = wrapException(e, request);
        } finally {
            response.close();
        }

    } catch (Exception e) {
        log.error("Error executing request to uri " + request.getURI(), e);
        exception = wrapException(e, request);
    } finally {
        try {
            client.close();
        } catch (Exception e) {
            log.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

}

From source file:org.flowable.admin.service.engine.FlowableClientService.java

public String executeRequestAsString(HttpUriRequest request, ServerConfig serverConfig,
        int expectedStatusCode) {

    FlowableServiceException exception = null;
    String result = null;//from   w w  w .j  a  v a2 s  .  c  om
    CloseableHttpClient client = getHttpClient(serverConfig);
    try {
        CloseableHttpResponse response = client.execute(request);
        boolean success = response.getStatusLine() != null
                && response.getStatusLine().getStatusCode() == expectedStatusCode;
        if (success) {
            result = IOUtils.toString(response.getEntity().getContent());
        } else {
            String errorMessage = null;
            try {
                if (response.getEntity().getContentLength() != 0) {
                    InputStream responseContent = response.getEntity().getContent();
                    JsonNode errorBody = objectMapper.readTree(responseContent);
                    errorMessage = extractError(errorBody,
                            "An error occurred while calling Flowable: " + response.getStatusLine());
                } else {
                    errorMessage = "An error was returned when calling the Activiti server";
                }
            } catch (Exception e) {
                log.warn("Error consuming response from uri " + request.getURI(), e);
                exception = wrapException(e, request);

            } finally {
                response.close();
            }
            exception = new FlowableServiceException(errorMessage);
        }
    } catch (Exception e) {
        log.error("Error executing request to uri " + request.getURI(), e);
        exception = wrapException(e, request);

    } finally {
        try {
            client.close();
        } catch (Exception e) {
            // No need to throw upwards, as this may hide exceptions/valid result
            log.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

    return result;
}

From source file:org.flowable.admin.service.engine.FlowableClientService.java

/**
 * Execute the given request, without using the response body. In case the
 * response returns a different status-code than expected, an
 * {@link FlowableServiceException} is thrown with the error message received
 * from the client, if possible.//from  w w  w.j av  a 2  s. c  o m
 */
public void executeRequestNoResponseBody(HttpUriRequest request, ServerConfig serverConfig,
        int expectedStatusCode) {

    FlowableServiceException exception = null;

    CloseableHttpClient client = getHttpClient(serverConfig);
    try {
        CloseableHttpResponse response = client.execute(request);
        boolean success = response.getStatusLine() != null
                && response.getStatusLine().getStatusCode() == expectedStatusCode;

        if (!success) {
            String errorMessage = null;
            try {
                if (response.getEntity() != null && response.getEntity().getContentLength() != 0) {
                    InputStream responseContent = response.getEntity().getContent();
                    JsonNode errorBody = objectMapper.readTree(responseContent);
                    errorMessage = extractError(errorBody,
                            "An error occurred while calling Flowable: " + response.getStatusLine());

                } else {
                    errorMessage = "An error was returned when calling the Activiti server";
                }
            } catch (Exception e) {
                log.warn("Error consuming response from uri " + request.getURI(), e);
                exception = wrapException(e, request);
            } finally {
                response.close();
            }
            exception = new FlowableServiceException(errorMessage);
        }
    } catch (Exception e) {
        log.error("Error executing request to uri " + request.getURI(), e);
        exception = wrapException(e, request);

    } finally {
        try {
            client.close();
        } catch (Exception e) {
            // No need to throw upwards, as this may hide exceptions/valid result
            log.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }
}

From source file:org.flowable.ui.admin.service.engine.FlowableClientService.java

/**
 * Execute the given request. Will return the parsed JSON present in the response-body, in case the status code is as expected. In case the response returns a different status-code, an
 * {@link FlowableServiceException} is thrown with the error message received from the client, if possible.
 *//*w w w  .  j a  va 2s .co m*/
public JsonNode executeRequest(HttpUriRequest request, String userName, String password,
        int expectedStatusCode) {

    FlowableServiceException exception = null;
    CloseableHttpClient client = getHttpClient(userName, password);
    try {
        try (CloseableHttpResponse response = client.execute(request)) {
            InputStream responseContent = response.getEntity().getContent();
            String strResponse = IOUtils.toString(responseContent, "utf-8");

            boolean success = response.getStatusLine() != null
                    && response.getStatusLine().getStatusCode() == expectedStatusCode;
            if (success) {
                JsonNode bodyNode = objectMapper.readTree(strResponse);
                return bodyNode;

            } else {
                JsonNode bodyNode = null;
                try {
                    bodyNode = objectMapper.readTree(strResponse);
                } catch (Exception e) {
                    LOGGER.debug("Error parsing error message", e);
                }
                exception = new FlowableServiceException(extractError(bodyNode,
                        "An error occurred while calling Flowable: " + response.getStatusLine()));
            }
        } catch (Exception e) {
            LOGGER.warn("Error consuming response from uri {}", request.getURI(), e);
            exception = wrapException(e, request);
        }
    } catch (Exception e) {
        LOGGER.error("Error executing request to uri {}", request.getURI(), e);
        exception = wrapException(e, request);
    } finally {
        try {
            client.close();
        } catch (Exception e) {
            LOGGER.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

    return null;
}

From source file:org.flowable.ui.admin.service.engine.FlowableClientService.java

public JsonNode executeDownloadRequest(HttpUriRequest request, HttpServletResponse httpResponse,
        String userName, String password, int expectedStatusCode) {

    FlowableServiceException exception = null;
    CloseableHttpClient client = getHttpClient(userName, password);
    try {//from  ww  w .ja  va 2s  .  c  o  m
        try (CloseableHttpResponse response = client.execute(request)) {
            boolean success = response.getStatusLine() != null
                    && response.getStatusLine().getStatusCode() == expectedStatusCode;
            if (success) {
                httpResponse.setHeader("Content-Disposition",
                        response.getHeaders("Content-Disposition")[0].getValue());
                response.getEntity().writeTo(httpResponse.getOutputStream());
                return null;

            } else {
                JsonNode bodyNode = null;
                String strResponse = IOUtils.toString(response.getEntity().getContent(), "utf-8");
                try {
                    bodyNode = objectMapper.readTree(strResponse);
                } catch (Exception e) {
                    LOGGER.debug("Error parsing error message", e);
                }
                exception = new FlowableServiceException(extractError(bodyNode,
                        "An error occurred while calling Flowable: " + response.getStatusLine()));
            }
        } catch (Exception e) {
            LOGGER.warn("Error consuming response from uri {}", request.getURI(), e);
            exception = wrapException(e, request);
        }
    } catch (Exception e) {
        LOGGER.error("Error executing request to uri {}", request.getURI(), e);
        exception = wrapException(e, request);
    } finally {
        try {
            client.close();
        } catch (Exception e) {
            LOGGER.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

    return null;
}

From source file:org.flowable.ui.admin.service.engine.FlowableClientService.java

public AttachmentResponseInfo executeDownloadRequest(HttpUriRequest request, String userName, String password,
        Integer... expectedStatusCodes) {
    FlowableServiceException exception = null;
    CloseableHttpClient client = getHttpClient(userName, password);
    try {/*from   ww  w .  ja  va  2 s  . c om*/
        try (CloseableHttpResponse response = client.execute(request)) {
            int statusCode = -1;
            if (response.getStatusLine() != null) {
                statusCode = response.getStatusLine().getStatusCode();
            }
            boolean success = Arrays.asList(expectedStatusCodes).contains(statusCode);
            if (success) {
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    String contentDispositionFileName[] = response.getHeaders("Content-Disposition")[0]
                            .getValue().split("=");
                    String fileName = contentDispositionFileName[contentDispositionFileName.length - 1];
                    return new AttachmentResponseInfo(fileName,
                            IOUtils.toByteArray(response.getEntity().getContent()));
                } else {
                    return new AttachmentResponseInfo(statusCode,
                            readJsonContent(response.getEntity().getContent()));
                }

            } else {
                exception = new FlowableServiceException(
                        extractError(readJsonContent(response.getEntity().getContent()),
                                "An error occurred while calling Flowable: " + response.getStatusLine()));
            }
        } catch (Exception e) {
            LOGGER.warn("Error consuming response from uri {}", request.getURI(), e);
            exception = wrapException(e, request);
        }
    } catch (Exception e) {
        LOGGER.error("Error executing request to uri {}", request.getURI(), e);
        exception = wrapException(e, request);
    } finally {
        try {
            client.close();
        } catch (Exception e) {
            LOGGER.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

    return null;
}

From source file:org.flowable.ui.admin.service.engine.FlowableClientService.java

public ResponseInfo execute(HttpUriRequest request, String userName, String password,
        int... expectedStatusCodes) {

    FlowableServiceException exception = null;
    CloseableHttpClient client = getHttpClient(userName, password);
    try {/*w w w.  j  ava 2 s .  co  m*/
        try (CloseableHttpResponse response = client.execute(request)) {
            JsonNode bodyNode = readJsonContent(response.getEntity().getContent());

            int statusCode = -1;
            if (response.getStatusLine() != null) {
                statusCode = response.getStatusLine().getStatusCode();
            }
            boolean success = Arrays.asList(expectedStatusCodes).contains(statusCode);

            if (success) {
                return new ResponseInfo(statusCode, bodyNode);

            } else {
                exception = new FlowableServiceException(
                        extractError(readJsonContent(response.getEntity().getContent()),
                                "An error occurred while calling Flowable: " + response.getStatusLine()));
            }
        } catch (Exception e) {
            LOGGER.warn("Error consuming response from uri {}", request.getURI(), e);
            exception = wrapException(e, request);
        }
    } catch (Exception e) {
        LOGGER.error("Error executing request to uri {}", request.getURI(), e);
        exception = wrapException(e, request);
    } finally {
        try {
            client.close();
        } catch (Exception e) {
            LOGGER.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

    return null;
}

From source file:org.flowable.ui.admin.service.engine.FlowableClientService.java

public void execute(HttpUriRequest request, HttpServletResponse httpResponse, String userName,
        String password) {//from www  .  ja  v  a  2  s  .  c  o  m

    FlowableServiceException exception = null;
    CloseableHttpClient client = getHttpClient(userName, password);
    try {
        try (CloseableHttpResponse response = client.execute(request)) {
            if (response.getStatusLine() != null
                    && response.getStatusLine().getStatusCode() != HttpStatus.SC_UNAUTHORIZED) {
                httpResponse.setStatus(response.getStatusLine().getStatusCode());
                if (response.getEntity() != null && response.getEntity().getContentType() != null) {
                    httpResponse.setContentType(response.getEntity().getContentType().getValue());
                    response.getEntity().writeTo(httpResponse.getOutputStream());
                }
            } else {
                exception = new FlowableServiceException(
                        extractError(readJsonContent(response.getEntity().getContent()),
                                "An error occurred while calling Flowable: " + response.getStatusLine()));
            }
        } catch (Exception e) {
            LOGGER.warn("Error consuming response from uri {}", request.getURI(), e);
            exception = wrapException(e, request);
        }
    } catch (Exception e) {
        LOGGER.error("Error executing request to uri {}", request.getURI(), e);
        exception = wrapException(e, request);
    } finally {
        try {
            client.close();
        } catch (Exception e) {
            LOGGER.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

}

From source file:org.flowable.ui.admin.service.engine.FlowableClientService.java

public String executeRequestAsString(HttpUriRequest request, ServerConfig serverConfig,
        int expectedStatusCode) {

    FlowableServiceException exception = null;
    String result = null;/*from w w  w  .ja va 2  s .  c  o  m*/
    CloseableHttpClient client = getHttpClient(serverConfig);
    try {
        CloseableHttpResponse response = client.execute(request);
        boolean success = response.getStatusLine() != null
                && response.getStatusLine().getStatusCode() == expectedStatusCode;
        if (success) {
            result = IOUtils.toString(response.getEntity().getContent(), "utf-8");
        } else {
            String errorMessage = null;
            try {
                if (response.getEntity().getContentLength() != 0) {
                    InputStream responseContent = response.getEntity().getContent();
                    JsonNode errorBody = objectMapper.readTree(responseContent);
                    errorMessage = extractError(errorBody,
                            "An error occurred while calling Flowable: " + response.getStatusLine());
                } else {
                    errorMessage = "An error was returned when calling the Flowable server";
                }
            } catch (Exception e) {
                LOGGER.warn("Error consuming response from uri {}", request.getURI(), e);
                exception = wrapException(e, request);

            } finally {
                response.close();
            }
            exception = new FlowableServiceException(errorMessage);
        }
    } catch (Exception e) {
        LOGGER.error("Error executing request to uri {}", request.getURI(), e);
        exception = wrapException(e, request);

    } finally {
        try {
            client.close();
        } catch (Exception e) {
            // No need to throw upwards, as this may hide exceptions/valid result
            LOGGER.warn("Error closing http client instance", e);
        }
    }

    if (exception != null) {
        throw exception;
    }

    return result;
}