001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.webservice;
021:
022: import java.io.BufferedReader;
023: import java.io.ByteArrayInputStream;
024: import java.io.CharArrayWriter;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.InputStreamReader;
028: import java.io.Reader;
029: import java.io.StringReader;
030: import java.util.Enumeration;
031:
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpSession;
034:
035: /**
036: * @author mleidig@schlund.de
037: */
038: public class HttpServiceRequest implements ServiceRequest {
039:
040: HttpServletRequest httpRequest;
041: String serviceName;
042: String requestURI;
043: String cachedMessage;
044:
045: public HttpServiceRequest(HttpServletRequest httpRequest) {
046: this .httpRequest = httpRequest;
047: }
048:
049: public String getServiceName() {
050: if (serviceName == null) {
051: serviceName = extractServiceName(httpRequest);
052: if (serviceName == null)
053: throw new IllegalArgumentException(
054: "No service name found.");
055: }
056: return serviceName;
057: }
058:
059: public static String extractServiceName(
060: HttpServletRequest httpRequest) {
061: String extName = null;
062: String name = httpRequest.getPathInfo();
063: if (name != null) {
064: int ind = name.lastIndexOf('/');
065: if (ind > -1) {
066: if ((name.length() - ind) > 1) {
067: extName = name.substring(ind + 1);
068: }
069: }
070: }
071: return extName;
072: }
073:
074: public Object getUnderlyingRequest() {
075: return httpRequest;
076: }
077:
078: public String getParameter(String name) {
079: return httpRequest.getParameter(name);
080: }
081:
082: public String getCharacterEncoding() {
083: return httpRequest.getCharacterEncoding();
084: }
085:
086: public String getMessage() throws IOException {
087: if (httpRequest.getContentType().equals(
088: Constants.CONTENT_TYPE_URLENCODED)) {
089: return httpRequest.getParameter("message");
090: } else {
091: if (cachedMessage == null) {
092: //Check if content type is text/plain text/xml application/xml ?
093: String charset = httpRequest.getCharacterEncoding();
094: if (charset == null)
095: charset = "UTF-8";
096: BufferedReader in = new BufferedReader(
097: new InputStreamReader(httpRequest
098: .getInputStream(), charset));
099: CharArrayWriter data = new CharArrayWriter();
100: char buf[] = new char[4096];
101: int ret;
102: while ((ret = in.read(buf, 0, 4096)) != -1)
103: data.write(buf, 0, ret);
104: cachedMessage = data.toString();
105: }
106: return cachedMessage;
107: }
108: }
109:
110: public Reader getMessageReader() throws IOException {
111: if (httpRequest.getContentType().equals(
112: Constants.CONTENT_TYPE_URLENCODED)) {
113: String msg = httpRequest.getParameter("message");
114: if (msg == null)
115: return null;
116: return new StringReader(msg);
117: } else {
118: //Check if content type is text/plain text/xml application/xml ?
119: String charset = httpRequest.getCharacterEncoding();
120: if (charset == null)
121: charset = "UTF-8";
122: return new InputStreamReader(httpRequest.getInputStream(),
123: charset);
124: }
125: }
126:
127: public InputStream getMessageStream() throws IOException {
128: if (httpRequest.getContentType().equals(
129: Constants.CONTENT_TYPE_URLENCODED)) {
130: String msg = httpRequest.getParameter("message");
131: if (msg == null)
132: return null;
133: String charset = httpRequest.getCharacterEncoding();
134: if (charset == null)
135: charset = "UTF-8";
136: byte[] bytes = msg.getBytes(charset);
137: return new ByteArrayInputStream(bytes);
138: } else {
139: //Check if content type is text/plain text/xml application/xml ?
140: return httpRequest.getInputStream();
141: }
142: }
143:
144: public String getRequestURI() {
145: if (requestURI == null) {
146: requestURI = getRequestURI(httpRequest);
147: }
148: return requestURI;
149: }
150:
151: private static String getRequestURI(HttpServletRequest srvReq) {
152: StringBuffer sb = new StringBuffer();
153: sb.append(srvReq.getScheme());
154: sb.append("://");
155: sb.append(srvReq.getServerName());
156: sb.append(":");
157: sb.append(srvReq.getServerPort());
158: sb.append(srvReq.getRequestURI());
159: HttpSession session = srvReq.getSession(false);
160: if (session != null) {
161: sb.append(Constants.SESSION_PREFIX);
162: sb.append(session.getId());
163: }
164: String s = srvReq.getQueryString();
165: if (s != null && !s.equals("")) {
166: sb.append("?");
167: sb.append(s);
168: }
169: return sb.toString();
170: }
171:
172: public String getServerName() {
173: return httpRequest.getServerName();
174: }
175:
176: private void dumpHeaders(StringBuilder sb) {
177: Enumeration<?> names = httpRequest.getHeaderNames();
178: while (names.hasMoreElements()) {
179: String name = (String) names.nextElement();
180: Enumeration<?> values = httpRequest.getHeaders(name);
181: while (values.hasMoreElements()) {
182: String value = (String) values.nextElement();
183: sb.append(name + ": " + value + "\n");
184: }
185: }
186: }
187:
188: private void dumpMessage(StringBuilder sb) {
189: if (cachedMessage == null) {
190: sb.append("[Message not available]");
191: } else {
192: sb.append(cachedMessage);
193: }
194: }
195:
196: public String dump() {
197: StringBuilder sb = new StringBuilder();
198: sb.append(getRequestURI());
199: sb.append("\n\n");
200: dumpHeaders(sb);
201: sb.append("\n");
202: dumpMessage(sb);
203: return sb.toString();
204: }
205:
206: }
|