001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.axis;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedInputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedOutputStream;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.InitialThreadLocal;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.util.SystemProperties;
025    
026    import java.io.InputStream;
027    import java.io.OutputStream;
028    
029    import java.net.Authenticator;
030    import java.net.HttpURLConnection;
031    import java.net.URL;
032    import java.net.URLConnection;
033    
034    import java.util.regex.Pattern;
035    
036    import org.apache.axis.AxisFault;
037    import org.apache.axis.Message;
038    import org.apache.axis.MessageContext;
039    import org.apache.axis.transport.http.HTTPConstants;
040    import org.apache.axis.transport.http.HTTPSender;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class SimpleHTTPSender extends HTTPSender {
046    
047            public static String getCurrentCookie() {
048                    return _currentCookie.get();
049            }
050    
051            public void invoke(MessageContext ctx) throws AxisFault {
052                    String url = ctx.getStrProp(MessageContext.TRANS_URL);
053    
054                    if (_pattern.matcher(url).matches()) {
055                            if (_log.isDebugEnabled()) {
056                                    _log.debug("A match was found for " + url);
057                            }
058    
059                            _invoke(ctx, url);
060                    }
061                    else {
062                            if (_log.isDebugEnabled()) {
063                                    _log.debug("No match was found for " + url);
064                            }
065    
066                            super.invoke(ctx);
067    
068                            _registerCurrentCookie(ctx);
069                    }
070            }
071    
072            private void _invoke(MessageContext ctx, String url) throws AxisFault {
073                    try {
074                            String userName = ctx.getUsername();
075                            String password = ctx.getPassword();
076    
077                            if ((userName != null) && (password != null)) {
078                                    Authenticator.setDefault(
079                                            new SimpleAuthenticator(userName, password));
080                            }
081    
082                            URL urlObj = new URL(url);
083    
084                            URLConnection urlc = urlObj.openConnection();
085    
086                            _writeToConnection(urlc, ctx);
087                            _readFromConnection(urlc, ctx);
088                    }
089                    catch (Exception e) {
090                            throw AxisFault.makeFault(e);
091                    }
092                    finally {
093                            Authenticator.setDefault(null);
094                    }
095            }
096    
097            private void _readFromConnection(URLConnection urlc, MessageContext ctx)
098                    throws Exception {
099    
100                    String contentType = urlc.getContentType();
101                    String contentLocation = urlc.getHeaderField("Content-Location");
102    
103                    InputStream is = ((HttpURLConnection)urlc).getErrorStream();
104    
105                    if (is == null) {
106                            is = urlc.getInputStream();
107                    }
108    
109                    is = new UnsyncBufferedInputStream(is, 8192);
110    
111                    Message response = new Message(is, false, contentType, contentLocation);
112    
113                    response.setMessageType(Message.RESPONSE);
114    
115                    ctx.setResponseMessage(response);
116            }
117    
118            private void _registerCurrentCookie(MessageContext ctx) {
119                    String cookie = StringPool.BLANK;
120    
121                    try {
122                            cookie = GetterUtil.getString(
123                                    ctx.getStrProp(HTTPConstants.HEADER_COOKIE));
124                    }
125                    catch (Throwable t) {
126                            _log.warn(t);
127                    }
128    
129                    _currentCookie.set(cookie);
130            }
131    
132            private void _writeToConnection(URLConnection urlc, MessageContext ctx)
133                    throws Exception {
134    
135                    urlc.setDoOutput(true);
136    
137                    Message request = ctx.getRequestMessage();
138    
139                    String contentType = request.getContentType(ctx.getSOAPConstants());
140    
141                    urlc.setRequestProperty("Content-Type", contentType);
142    
143                    if (ctx.useSOAPAction()) {
144                            urlc.setRequestProperty("SOAPAction", ctx.getSOAPActionURI());
145                    }
146    
147                    OutputStream os = new UnsyncBufferedOutputStream(
148                            urlc.getOutputStream(), 8192);
149    
150                    request.writeTo(os);
151    
152                    os.flush();
153            }
154    
155            private static Log _log = LogFactoryUtil.getLog(SimpleHTTPSender.class);
156    
157            private static ThreadLocal<String> _currentCookie =
158                    new InitialThreadLocal<String>(
159                            SimpleHTTPSender.class + "._currentCookie", StringPool.BLANK);
160            private static Pattern _pattern = Pattern.compile(
161                    SystemProperties.get(
162                            SimpleHTTPSender.class.getName() + ".regexp.pattern"));
163    
164    }