private void copyProtocolHeaders(Message request, Message response) { if (request.getHeader(Exchange.CONTENT_ENCODING) != null) { String contentEncoding = request.getHeader(Exchange.CONTENT_ENCODING, String.class); response.setHeader(Exchange.CONTENT_ENCODING, contentEncoding); } if (checkChunked(response, response.getExchange())) { response.setHeader(Exchange.TRANSFER_ENCODING, "chunked"); } }
protected String convertHeaderValueToString(Exchange exchange, Object headerValue) { if ((headerValue instanceof Date || headerValue instanceof Locale) && convertDateAndLocaleLocally(exchange)) { if (headerValue instanceof Date) { return toHttpDate((Date)headerValue); } else { return toHttpLanguage((Locale)headerValue); } } else { return exchange.getContext().getTypeConverter().convertTo(String.class, headerValue); } }
String headerValue = convertHeaderValueToString(exchange, it.next()); if (headerValue != null && headerFilterStrategy != null && !headerFilterStrategy.applyFilterToCamelHeaders(key, headerValue, exchange)) { doWriteGZIPResponse(message, response, exchange); } else { doWriteDirectResponse(message, response, exchange);
public void writeResponse(Exchange exchange, HttpServletResponse response) throws IOException { Message target = exchange.hasOut() ? exchange.getOut() : exchange.getIn(); if (exchange.isFailed()) { if (exchange.getException() != null) { doWriteExceptionResponse(exchange.getException(), response); } else { // it must be a fault, no need to check for the fault flag on the message doWriteFaultResponse(target, response, exchange); } } else { if (exchange.hasOut()) { // just copy the protocol relates header if we do not have them copyProtocolHeaders(exchange.getIn(), exchange.getOut()); } doWriteResponse(target, response, exchange); } }
if (allowJavaSerializedObject || isTransferException()) { try { Object object = message.getMandatoryBody(Serializable.class); if (checkChunked(message, exchange)) { is = message.getBody(InputStream.class); } else { if (!isText(contentType)) { is = exchange.getContext().getTypeConverter().tryConvertTo(InputStream.class, message.getBody()); if (!checkChunked(message, exchange)) { CachedOutputStream stream = new CachedOutputStream(exchange); try { int len = copyStream(is, stream, response.getBufferSize()); LOG.debug("Streaming response in non-chunked mode with content-length {} and buffer size: {}", len, len); copyStream(stream.getInputStream(), os, len); LOG.debug("Streaming response in chunked mode with buffer size {}", response.getBufferSize()); copyStream(is, os, response.getBufferSize());
public HttpBinding getHttpBinding() { if (httpBinding == null) { // create a new binding and use the options from this endpoint httpBinding = new DefaultHttpBinding(); httpBinding.setHeaderFilterStrategy(getHeaderFilterStrategy()); httpBinding.setTransferException(isTransferException()); if (getComponent() != null) { httpBinding.setAllowJavaSerializedObject(getComponent().isAllowJavaSerializedObject()); } httpBinding.setEagerCheckContentAvailable(isEagerCheckContentAvailable()); httpBinding.setMapHttpMessageBody(isMapHttpMessageBody()); httpBinding.setMapHttpMessageHeaders(isMapHttpMessageHeaders()); httpBinding.setMapHttpMessageFormUrlEncodedBody(isMapHttpMessageFormUrlEncodedBody()); } return httpBinding; }
@Override protected void populateRequestParameters(HttpServletRequest request, HttpMessage message) throws Exception { super.populateRequestParameters(request, message); String path = request.getPathInfo(); if (path == null) { return; } // in the endpoint the user may have defined rest {} placeholders // so we need to map those placeholders with data from the incoming request context path ServletEndpoint endpoint = (ServletEndpoint) message.getExchange().getFromEndpoint(); String consumerPath = endpoint.getPath(); if (useRestMatching(consumerPath)) { // split using single char / is optimized in the jdk String[] paths = path.split("/"); String[] consumerPaths = consumerPath.split("/"); for (int i = 0; i < consumerPaths.length; i++) { if (paths.length < i) { break; } String p1 = consumerPaths[i]; if (p1.startsWith("{") && p1.endsWith("}")) { String key = p1.substring(1, p1.length() - 1); String value = paths[i]; if (value != null) { message.setHeader(key, value); } } } } }
@Override public HttpBinding getHttpBinding() { // make sure we include servlet variant of the http binding if (this.binding == null) { // is attachment binding enabled? if (isAttachmentMultipartBinding()) { this.binding = new AttachmentHttpBinding(); } else { this.binding = new DefaultHttpBinding(); } this.binding.setFileNameExtWhitelist(getFileNameExtWhitelist()); this.binding.setTransferException(isTransferException()); if (getComponent() != null) { this.binding.setAllowJavaSerializedObject(getComponent().isAllowJavaSerializedObject()); } this.binding.setHeaderFilterStrategy(getHeaderFilterStrategy()); this.binding.setEagerCheckContentAvailable(isEagerCheckContentAvailable()); this.binding.setMapHttpMessageBody(isMapHttpMessageBody()); this.binding.setMapHttpMessageHeaders(isMapHttpMessageHeaders()); this.binding.setMapHttpMessageFormUrlEncodedBody(isMapHttpMessageFormUrlEncodedBody()); } return this.binding; }
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); } }