Class HttpHelper


  • public final class HttpHelper
    extends Object
    • Method Detail

      • isSecureConnection

        public static boolean isSecureConnection​(String uri)
      • setCharsetFromContentType

        public static void setCharsetFromContentType​(String contentType,
                                                     org.apache.camel.Exchange exchange)
      • getCharsetFromContentType

        public static String getCharsetFromContentType​(String contentType)
      • writeObjectToServletResponse

        public static void writeObjectToServletResponse​(javax.servlet.ServletResponse response,
                                                        Object target)
                                                 throws IOException
        Writes the given object as response body to the servlet response

        The content type will be set to HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT

        Parameters:
        response - servlet response
        target - object to write
        Throws:
        IOException - is thrown if error writing
      • writeObjectToStream

        public static void writeObjectToStream​(OutputStream stream,
                                               Object target)
                                        throws IOException
        Writes the given object as response body to the output stream
        Parameters:
        stream - output stream
        target - object to write
        Throws:
        IOException - is thrown if error writing
      • deserializeJavaObjectFromStream

        @Deprecated
        public static Object deserializeJavaObjectFromStream​(InputStream is)
                                                      throws ClassNotFoundException,
                                                             IOException
        Deprecated.
        Camel 3.0 Please use the one which has the parameter of camel context
        Deserializes the input stream to a Java object
        Parameters:
        is - input stream for the Java object
        Returns:
        the java object, or null if input stream was null
        Throws:
        ClassNotFoundException - is thrown if class not found
        IOException - can be thrown
      • deserializeJavaObjectFromStream

        public static Object deserializeJavaObjectFromStream​(InputStream is,
                                                             org.apache.camel.CamelContext context)
                                                      throws ClassNotFoundException,
                                                             IOException
        Deserializes the input stream to a Java object
        Parameters:
        is - input stream for the Java object
        context - the camel context which could help us to apply the customer classloader
        Returns:
        the java object, or null if input stream was null
        Throws:
        ClassNotFoundException - is thrown if class not found
        IOException - can be thrown
      • readRequestBodyFromServletRequest

        public static Object readRequestBodyFromServletRequest​(javax.servlet.http.HttpServletRequest request,
                                                               org.apache.camel.Exchange exchange)
                                                        throws IOException
        Reads the request body from the given http servlet request.
        Parameters:
        request - http servlet request
        exchange - the exchange
        Returns:
        the request body, can be null if no body
        Throws:
        IOException - is thrown if error reading request body
      • cacheResponseBodyFromInputStream

        public static Object cacheResponseBodyFromInputStream​(InputStream is,
                                                              org.apache.camel.Exchange exchange)
                                                       throws IOException
        Caches the response body from the given input stream, which is needed by PollingConsumer.
        Parameters:
        is - the input stream
        exchange - the exchange
        Returns:
        the cached response body
        Throws:
        IOException - is thrown if error reading response body
      • createURL

        public static String createURL​(org.apache.camel.Exchange exchange,
                                       HttpCommonEndpoint endpoint)
        Creates the URL to invoke.
        Parameters:
        exchange - the exchange
        endpoint - the endpoint
        Returns:
        the URL to invoke
      • createURI

        public static URI createURI​(org.apache.camel.Exchange exchange,
                                    String url,
                                    HttpCommonEndpoint endpoint)
                             throws URISyntaxException
        Creates the URI to invoke.
        Parameters:
        exchange - the exchange
        url - the url to invoke
        endpoint - the endpoint
        Returns:
        the URI to invoke
        Throws:
        URISyntaxException
      • appendHeader

        public static void appendHeader​(Map<String,​Object> headers,
                                        String key,
                                        Object value)
        Appends the key/value to the headers.

        This implementation supports keys with multiple values. In such situations the value will be a List that contains the multiple values.

        Parameters:
        headers - headers
        key - the key
        value - the value
      • extractHttpParameterValue

        public static Object extractHttpParameterValue​(String value)
        Extracts the parameter value.

        This implementation supports HTTP multi value parameters which is based on the syntax of [value1, value2, value3] by returning a List containing the values.

        If the value is not a HTTP mulit value the value is returned as is.

        Parameters:
        value - the parameter value
        Returns:
        the extracted parameter value, see more details in javadoc.
      • createMethod

        public static HttpMethods createMethod​(org.apache.camel.Exchange exchange,
                                               HttpCommonEndpoint endpoint,
                                               boolean hasPayload)
                                        throws URISyntaxException
        Creates the HttpMethod to use to call the remote server, often either its GET or POST.
        Parameters:
        exchange - the exchange
        Returns:
        the created method
        Throws:
        URISyntaxException
      • isStatusCodeOk

        public static boolean isStatusCodeOk​(int statusCode,
                                             String okStatusCodeRange)
        Checks whether the given http status code is within the ok range
        Parameters:
        statusCode - the status code
        okStatusCodeRange - the ok range (inclusive)
        Returns:
        true if ok, false otherwise