001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.axis;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.util.SystemProperties;
026:
027: import java.io.BufferedInputStream;
028: import java.io.BufferedOutputStream;
029: import java.io.InputStream;
030: import java.io.OutputStream;
031:
032: import java.net.Authenticator;
033: import java.net.HttpURLConnection;
034: import java.net.URL;
035: import java.net.URLConnection;
036:
037: import java.util.regex.Pattern;
038:
039: import org.apache.axis.AxisFault;
040: import org.apache.axis.Message;
041: import org.apache.axis.MessageContext;
042: import org.apache.axis.transport.http.HTTPConstants;
043: import org.apache.axis.transport.http.HTTPSender;
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046:
047: /**
048: * <a href="SimpleHTTPSender.java.html"><b><i>View Source</i></b></a>
049: *
050: * @author Brian Wing Shun Chan
051: *
052: */
053: public class SimpleHTTPSender extends HTTPSender {
054:
055: public static final String URL_REGEXP_PATTERN = GetterUtil
056: .getString(SystemProperties.get(SimpleHTTPSender.class
057: .getName()
058: + ".regexp.pattern"));
059:
060: public static final Pattern URL_PATTERN = Pattern
061: .compile(URL_REGEXP_PATTERN);
062:
063: public static String getCurrentCookie() {
064: return (String) _currentCookie.get();
065: }
066:
067: public void invoke(MessageContext ctx) throws AxisFault {
068: String url = ctx.getStrProp(MessageContext.TRANS_URL);
069:
070: if (URL_PATTERN.matcher(url).matches()) {
071: if (_log.isDebugEnabled()) {
072: _log.debug("A match was found for " + url);
073: }
074:
075: _invoke(ctx, url);
076: } else {
077: if (_log.isDebugEnabled()) {
078: _log.debug("No match was found for " + url);
079: }
080:
081: super .invoke(ctx);
082:
083: _registerCurrentCookie(ctx);
084: }
085: }
086:
087: private void _invoke(MessageContext ctx, String url)
088: throws AxisFault {
089: try {
090: String userName = ctx.getUsername();
091: String password = ctx.getPassword();
092:
093: if ((userName != null) && (password != null)) {
094: Authenticator.setDefault(new SimpleAuthenticator(
095: userName, password));
096: }
097:
098: URL urlObj = new URL(url);
099:
100: URLConnection urlc = urlObj.openConnection();
101:
102: _writeToConnection(urlc, ctx);
103: _readFromConnection(urlc, ctx);
104: } catch (Exception e) {
105: throw AxisFault.makeFault(e);
106: } finally {
107: Authenticator.setDefault(null);
108: }
109: }
110:
111: private void _readFromConnection(URLConnection urlc,
112: MessageContext ctx) throws Exception {
113:
114: String contentType = urlc.getContentType();
115: String contentLocation = urlc
116: .getHeaderField("Content-Location");
117:
118: InputStream in = ((HttpURLConnection) urlc).getErrorStream();
119:
120: if (in == null) {
121: in = urlc.getInputStream();
122: }
123:
124: in = new BufferedInputStream(in, 8192);
125:
126: Message response = new Message(in, false, contentType,
127: contentLocation);
128:
129: response.setMessageType(Message.RESPONSE);
130:
131: ctx.setResponseMessage(response);
132: }
133:
134: private void _registerCurrentCookie(MessageContext ctx) {
135: String cookie = StringPool.BLANK;
136:
137: try {
138: cookie = GetterUtil.getString(ctx
139: .getStrProp(HTTPConstants.HEADER_COOKIE));
140: } catch (Throwable t) {
141: _log.warn(t);
142: }
143:
144: _currentCookie.set(cookie);
145: }
146:
147: private void _writeToConnection(URLConnection urlc,
148: MessageContext ctx) throws Exception {
149:
150: urlc.setDoOutput(true);
151:
152: Message request = ctx.getRequestMessage();
153:
154: String contentType = request.getContentType(ctx
155: .getSOAPConstants());
156:
157: urlc.setRequestProperty("Content-Type", contentType);
158:
159: if (ctx.useSOAPAction()) {
160: urlc.setRequestProperty("SOAPAction", ctx
161: .getSOAPActionURI());
162: }
163:
164: OutputStream out = new BufferedOutputStream(urlc
165: .getOutputStream(), 8192);
166:
167: request.writeTo(out);
168:
169: out.flush();
170: }
171:
172: private static ThreadLocal _currentCookie = new ThreadLocal() {
173: protected Object initialValue() {
174: return StringPool.BLANK;
175: }
176: };
177:
178: private static Log _log = LogFactory.getLog(SimpleHTTPSender.class);
179:
180: }
|