001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.http.jetty;
018
019 import java.io.ByteArrayInputStream;
020 import java.io.ByteArrayOutputStream;
021 import java.io.IOException;
022 import java.io.InputStreamReader;
023 import java.io.Reader;
024 import java.io.UnsupportedEncodingException;
025
026 import org.mortbay.io.Buffer;
027 import org.mortbay.io.BufferUtil;
028 import org.mortbay.jetty.HttpFields;
029 import org.mortbay.jetty.HttpHeaders;
030 import org.mortbay.jetty.client.HttpExchange;
031 import org.mortbay.util.StringUtil;
032
033 public class SmxHttpExchange extends HttpExchange {
034
035 int responseStatus;
036 HttpFields responseFields;
037 String encoding = "utf-8";
038 ByteArrayOutputStream responseContent;
039 int contentLength;
040
041 public SmxHttpExchange() {
042 responseFields = new HttpFields();
043 }
044
045 /* ------------------------------------------------------------ */
046 public int getResponseStatus() {
047 if (getStatus() < STATUS_PARSING_HEADERS) {
048 throw new IllegalStateException("Response not received");
049 }
050 return responseStatus;
051 }
052
053 /* ------------------------------------------------------------ */
054 public HttpFields getResponseFields() {
055 if (getStatus() < STATUS_PARSING_CONTENT) {
056 throw new IllegalStateException("Headers not complete");
057 }
058 return responseFields;
059 }
060
061 /* ------------------------------------------------------------ */
062 public String getResponseContent() throws UnsupportedEncodingException {
063 if (responseContent != null) {
064 return responseContent.toString(encoding);
065 }
066 return null;
067 }
068
069 /* ------------------------------------------------------------ */
070 public Reader getResponseReader() throws UnsupportedEncodingException {
071 if (responseContent != null) {
072 return new InputStreamReader(new ByteArrayInputStream(responseContent.toByteArray()), encoding);
073 }
074 return null;
075 }
076
077 /* ------------------------------------------------------------ */
078 public byte[] getResponseData() throws UnsupportedEncodingException {
079 if (responseContent != null) {
080 return responseContent.toByteArray();
081 }
082 return null;
083 }
084
085 /* ------------------------------------------------------------ */
086 public String getResponseEncoding() throws UnsupportedEncodingException {
087 return encoding;
088 }
089
090 /* ------------------------------------------------------------ */
091 protected void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException {
092 responseStatus = status;
093 }
094
095 /* ------------------------------------------------------------ */
096 protected void onResponseHeader(Buffer name, Buffer value) throws IOException {
097 if (responseFields != null) {
098 responseFields.add(name, value);
099 }
100 int header = HttpHeaders.CACHE.getOrdinal(value);
101 switch (header) {
102 case HttpHeaders.CONTENT_LANGUAGE_ORDINAL:
103 contentLength = BufferUtil.toInt(value);
104 break;
105 case HttpHeaders.CONTENT_TYPE_ORDINAL:
106 String mime = StringUtil.asciiToLowerCase(value.toString());
107 int i = mime.indexOf("charset=");
108 if (i > 0) {
109 mime = mime.substring(i + 8);
110 i = mime.indexOf(';');
111 if (i > 0) {
112 mime = mime.substring(0, i);
113 }
114 }
115 if (mime != null && mime.length() > 0) {
116 encoding = mime;
117 }
118 break;
119 default:
120 break;
121 }
122 }
123
124 /* ------------------------------------------------------------ */
125 protected void onResponseContent(Buffer content) throws IOException {
126 if (responseContent == null) {
127 responseContent = new ByteArrayOutputStream(contentLength);
128 }
129 content.writeTo(responseContent);
130 }
131
132 }