All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.predic8.membrane.core.util.HttpUtil Maven / Gradle / Ivy

There is a newer version: 5.6.0
Show newest version
/* Copyright 2009, 2012 predic8 GmbH, www.predic8.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

package com.predic8.membrane.core.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import com.predic8.membrane.core.http.*;
import com.predic8.membrane.core.transport.http.LineTooLongException;
import org.apache.commons.lang.StringEscapeUtils;

import com.predic8.membrane.core.Constants;
import com.predic8.membrane.core.http.Response.ResponseBuilder;
import com.predic8.membrane.core.transport.http.EOFWhileReadingLineException;

public class HttpUtil {

	private static DateFormat GMT_DATE_FORMAT = createGMTDateFormat();
	private final static int MAX_LINE_LENGTH;

	static {
		String maxLineLength = System.getProperty("membrane.core.http.body.maxlinelength");
		MAX_LINE_LENGTH = maxLineLength == null ? 8092 : Integer.parseInt(maxLineLength);
	}

	public static DateFormat createGMTDateFormat() {
		SimpleDateFormat GMT_DATE_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
		GMT_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
		return GMT_DATE_FORMAT;
	}

	public static String readLine(InputStream in) throws IOException, EndOfStreamException {

		StringBuilder line = new StringBuilder(128);

		int b;
		int l = 0;
		while ((b = in.read()) != -1) {
			if (b == 13) {
				in.read();
				return line.toString();
			}
			if (b == 10) {
				in.mark(2);
				if (in.read() != 13)
					in.reset();
				return line.toString();
			}

			line.append((char) b);
			if (++l == MAX_LINE_LENGTH)
				throw new LineTooLongException(line.toString());
		}

		throw new EOFWhileReadingLineException(line.toString());
	}


	public static int readChunkSize(InputStream in) throws IOException {
		StringBuilder buffer = new StringBuilder();

		int c = 0;
		while ((c = in.read()) != -1) {
			if (c == 13) {
				c = in.read();
				break;
			}

			// ignore chunk extensions
			if (c == ';') {
				while ((c = in.read()) != 10)
					;
			}

			buffer.append((char) c);
		}

		return Integer.parseInt(buffer.toString().trim(), 16);
	}

	public static Response setHTMLErrorResponse(ResponseBuilder responseBuilder, String message, String comment) {
		Response response = responseBuilder.build();
		response.setHeader(createHeaders(MimeType.TEXT_HTML_UTF8));
		response.setBodyContent(getHTMLErrorBody(message, comment).getBytes(Constants.UTF_8_CHARSET));
		return response;
	}

	private static String getHTMLErrorBody(String text, String comment) {
		StringBuilder buf = new StringBuilder(256);

		buf.append("\r\n" +
				"\r\n" +
				"\r\n" +
				"Internal Server Error\r\n" +
				"\r\n" +
				"" +
				"\r\n" +
				"

Internal Server Error

"); buf.append("
"); buf.append("

While processing your request, the following error was detected. "); buf.append(comment); buf.append("

\r\n"); buf.append("
");
		buf.append(StringEscapeUtils.escapeHtml(text));
		buf.append("
"); buf.append("

"); buf.append(Constants.HTML_FOOTER); buf.append("

"); buf.append(""); return buf.toString(); } public static Response createSOAPValidationErrorResponse(String message) { Response response = new Response(); response.setStatusCode(400); response.setStatusMessage("Bad request"); response.setHeader(createHeaders(MimeType.TEXT_XML_UTF8)); response.setBodyContent(getFaultSOAPBody(message).getBytes(Constants.UTF_8_CHARSET)); return response; } private static String getFaultSOAPBody(String text) { return getFaultSOAPBody("Message validation failed!", text); } public static String getFaultSOAPBody(String title, String text) { StringBuilder buf = new StringBuilder(256); buf.append(""); buf.append(Constants.CRLF); buf.append(""); buf.append(Constants.CRLF); buf.append(""); buf.append(Constants.CRLF); buf.append("soapenv:Server"); buf.append(Constants.CRLF); buf.append(""); buf.append(StringEscapeUtils.escapeXml(title)); buf.append(""); buf.append(Constants.CRLF); buf.append("" + StringEscapeUtils.escapeXml(text) + ""); buf.append(Constants.CRLF); buf.append(""); buf.append(Constants.CRLF); buf.append(""); buf.append(Constants.CRLF); buf.append(""); return buf.toString(); } public static String getFaultSOAP12Body(String title, String text) { StringBuilder buf = new StringBuilder(256); buf.append(""); buf.append(Constants.CRLF); buf.append(""); buf.append(Constants.CRLF); buf.append(""); buf.append(Constants.CRLF); buf.append(""); buf.append(Constants.CRLF); buf.append("soapenv:Receiver"); buf.append(Constants.CRLF); buf.append(""); buf.append(Constants.CRLF); buf.append(""); buf.append(StringEscapeUtils.escapeXml(title)); buf.append(""); buf.append(Constants.CRLF); buf.append("" + StringEscapeUtils.escapeXml(text) + ""); buf.append(Constants.CRLF); buf.append(""); buf.append(Constants.CRLF); buf.append(""); buf.append(Constants.CRLF); buf.append(""); return buf.toString(); } public static Response createResponse(int code, String msg, byte[] body, String contentType, String... headers) { Response res = new Response(); res.setStatusCode(code); res.setStatusMessage(msg); res.setHeader(createHeaders(contentType, headers)); if (body != null) res.setBodyContent(body); return res; } public static Header createHeaders(String contentType, String... headers) { Header header = new Header(); if (contentType != null ) header.setContentType(contentType); synchronized (GMT_DATE_FORMAT) { header.add("Date", GMT_DATE_FORMAT.format(new Date())); } header.add("Server", Constants.PRODUCT_NAME + " " + Constants.VERSION + ". See http://membrane-soa.org"); header.add("Connection", Header.CLOSE); for (int i = 0; i readChunks(InputStream in) throws IOException { List chunks = new ArrayList(); int chunkSize; while ((chunkSize = readChunkSize(in)) > 0) { chunks.add(new Chunk(ByteUtil.readByteArray(in, chunkSize))); in.read(); // CR in.read(); // LF } in.read(); // CR in.read(); // LF return chunks; } public static void readChunksAndDrop(InputStream in, List observers) throws IOException { int chunkSize; while ((chunkSize = readChunkSize(in)) > 0) { byte[] bytes = ByteUtil.readByteArray(in, chunkSize); Chunk chunk = new Chunk(bytes); for (MessageObserver observer : observers) observer.bodyChunk(chunk); in.read(); // CR in.read(); // LF } in.read(); // CR in.read(); // LF } public static String getHostName(String destination) throws MalformedURLException { return new URL(destination).getHost(); } public static String getPathAndQueryString(String dest) throws MalformedURLException { URL url = new URL(dest); String uri = url.getPath(); if (url.getQuery() != null) { return uri + "?" + url.getQuery(); } return uri; } public static int getPort(String hostAndPort) { return Integer.parseInt(hostAndPort.split(":")[1]); } public static int getPort(URL url) throws MalformedURLException { int port = url.getPort(); if (port == -1) { port = url.getDefaultPort(); if (port == -1) port = 80; } return port; } public static boolean isAbsoluteURI(String uri) { uri = uri.toLowerCase(); return uri.startsWith("http://") || uri.startsWith("https://"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy