protected void readHeaders(HttpServletRequest request, HttpMessage message) { LOG.trace("readHeaders {}", request); Map<String, Object> headers = message.getHeaders(); //apply the headerFilterStrategy Enumeration<?> names = request.getHeaderNames(); while (names.hasMoreElements()) { String name = (String)names.nextElement(); String value = request.getHeader(name); // use http helper to extract parameter value as it may contain multiple values Object extracted = HttpHelper.extractHttpParameterValue(value); // mapping the content-type if (name.toLowerCase().equals("content-type")) { name = Exchange.CONTENT_TYPE; } if (headerFilterStrategy != null && !headerFilterStrategy.applyFilterToExternalHeaders(name, extracted, message.getExchange())) { HttpHelper.appendHeader(headers, name, extracted); } } if (request.getCharacterEncoding() != null) { headers.put(Exchange.HTTP_CHARACTER_ENCODING, request.getCharacterEncoding()); message.getExchange().setProperty(Exchange.CHARSET_NAME, request.getCharacterEncoding()); } try { populateRequestParameters(request, message); } catch (Exception e) { throw new RuntimeCamelException("Cannot read request parameters due " + e.getMessage(), e); } }
/** * Deserializes the input stream to a Java object * * @param is input stream for the Java object * @return the java object, or <tt>null</tt> if input stream was <tt>null</tt> * @throws ClassNotFoundException is thrown if class not found * @throws IOException can be thrown * @deprecated Camel 3.0 * Please use the one which has the parameter of camel context */ @Deprecated public static Object deserializeJavaObjectFromStream(InputStream is) throws ClassNotFoundException, IOException { return deserializeJavaObjectFromStream(is, null); }
/** * Reads the response body from the given http servlet request. * * @param request http servlet request * @param exchange the exchange * @return the request body, can be <tt>null</tt> if no body * @throws IOException is thrown if error reading response body */ public static Object readRequestBodyFromServletRequest(HttpServletRequest request, Exchange exchange) throws IOException { InputStream is = HttpConverter.toInputStream(request, exchange); return readResponseBodyFromInputStream(is, exchange); }
contentType = header.getValue(); HttpHelper.setCharsetFromContentType(contentType, exchange); return HttpHelper.deserializeJavaObjectFromStream(is, exchange.getContext()); } else {
protected HttpMethod createMethod(Exchange exchange) throws Exception { String url = HttpHelper.createURL(exchange, getEndpoint()); URI uri = HttpHelper.createURI(exchange, url, getEndpoint()); String rewriteUrl = HttpHelper.urlRewrite(exchange, url, getEndpoint(), this); if (rewriteUrl != null) { String methodName = HttpHelper.createMethod(exchange, getEndpoint(), requestEntity != null).name(); HttpMethods methodsToUse = HttpMethods.valueOf(methodName); HttpMethod method = methodsToUse.createMethod(url);
protected void populateRequestParameters(HttpServletRequest request, HttpMessage message) throws Exception { //we populate the http request parameters without checking the request method Map<String, Object> headers = message.getHeaders(); Enumeration<?> names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String)names.nextElement(); // there may be multiple values for the same name String[] values = request.getParameterValues(name); LOG.trace("HTTP parameter {} = {}", name, values); if (values != null) { for (String value : values) { if (headerFilterStrategy != null && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, message.getExchange())) { HttpHelper.appendHeader(headers, name, value); } } } } }
HttpHelper.setCharsetFromContentType(request.getContentType(), exchange); exchange.setIn(new HttpMessage(exchange, consumer.getEndpoint(), request, response));
LOG.debug("Http responseCode: {}", code); final boolean ok = HttpHelper.isStatusCodeOk(code, "200-299"); if (!ok && throwExceptionOnFailure) {
/** * Writes the given object as response body to the servlet response * <p/> * The content type will be set to {@link HttpConstants#CONTENT_TYPE_JAVA_SERIALIZED_OBJECT} * * @param response servlet response * @param target object to write * @throws IOException is thrown if error writing */ public static void writeObjectToServletResponse(ServletResponse response, Object target) throws IOException { response.setContentType(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT); writeObjectToStream(response.getOutputStream(), target); }
if (headerFilterStrategy != null && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, message.getExchange())) { HttpHelper.appendHeader(headers, name, value);
HttpHelper.setCharsetFromContentType(request.getContentType(), exchange); exchange.setIn(new HttpMessage(exchange, consumer.getEndpoint(), request, response));
boolean ok = HttpHelper.isStatusCodeOk(responseCode, getEndpoint().getOkStatusCodeRange()); if (ok) {
HttpHelper.writeObjectToStream(bos, obj); answer = new ByteArrayRequestEntity(bos.toByteArray(), HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT); IOHelper.close(bos);
Object extracted = HttpHelper.extractHttpParameterValue(value); if (strategy != null && !strategy.applyFilterToExternalHeaders(name, extracted, exchange)) { HttpHelper.appendHeader(answer.getHeaders(), name, extracted);
HttpHelper.appendHeader(headers, name, value); if (getHeaderFilterStrategy() != null && !getHeaderFilterStrategy().applyFilterToExternalHeaders(name, value, message.getExchange())) { HttpHelper.appendHeader(headers, name, value);
String okCodes = getOption(parameters, "okStatusCodeRange", String.class).orElse("200-299"); if (!HttpHelper.isStatusCodeOk(code, okCodes)) { if (code == 401) {
Object body = HttpHelper.readResponseBodyFromInputStream(method.getResponseBodyAsStream(), exchange);
protected void readBody(HttpServletRequest request, HttpMessage message) { LOG.trace("readBody {}", request); // lets parse the body Object body = message.getBody(); // reset the stream cache if the body is the instance of StreamCache if (body instanceof StreamCache) { ((StreamCache) body).reset(); } // if content type is serialized java object, then de-serialize it to a Java object if (request.getContentType() != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(request.getContentType())) { // only deserialize java if allowed if (allowJavaSerializedObject || isTransferException()) { try { InputStream is = message.getExchange().getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, body); Object object = HttpHelper.deserializeJavaObjectFromStream(is, message.getExchange().getContext()); if (object != null) { message.setBody(object); } } catch (Exception e) { throw new RuntimeCamelException("Cannot deserialize body to Java object", e); } } else { // set empty body message.setBody(null); } } populateAttachments(request, message); }