001    /**
002     * Copyright (c) 2000-2013 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.SystemProperties;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.io.InputStream;
025    import java.io.OutputStream;
026    
027    import java.net.Authenticator;
028    import java.net.HttpURLConnection;
029    import java.net.URL;
030    import java.net.URLConnection;
031    
032    import java.util.regex.Matcher;
033    import java.util.regex.Pattern;
034    
035    import org.apache.axis.AxisFault;
036    import org.apache.axis.Message;
037    import org.apache.axis.MessageContext;
038    import org.apache.axis.transport.http.HTTPSender;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class SimpleHTTPSender extends HTTPSender {
044    
045            public SimpleHTTPSender() {
046                    String regexp = SystemProperties.get(
047                            SimpleHTTPSender.class.getName() + ".regexp.pattern");
048    
049                    if (Validator.isNotNull(regexp)) {
050                            _pattern = Pattern.compile(regexp);
051                    }
052            }
053    
054            @Override
055            public void invoke(MessageContext messageContext) throws AxisFault {
056                    String url = messageContext.getStrProp(MessageContext.TRANS_URL);
057    
058                    Matcher matcher = null;
059    
060                    if (_pattern != null) {
061                            matcher = _pattern.matcher(url);
062                    }
063    
064                    if ((matcher != null) && matcher.matches()) {
065                            if (_log.isDebugEnabled()) {
066                                    _log.debug("A match was found for " + url);
067                            }
068    
069                            _invoke(messageContext, url);
070                    }
071                    else {
072                            super.invoke(messageContext);
073                    }
074            }
075    
076            private void _invoke(MessageContext messageContext, String url)
077                    throws AxisFault {
078    
079                    try {
080                            String userName = messageContext.getUsername();
081                            String password = messageContext.getPassword();
082    
083                            if ((userName != null) && (password != null)) {
084                                    Authenticator.setDefault(
085                                            new SimpleAuthenticator(userName, password));
086                            }
087    
088                            URL urlObj = new URL(url);
089    
090                            URLConnection urlConnection = urlObj.openConnection();
091    
092                            _writeToConnection(urlConnection, messageContext);
093                            _readFromConnection(urlConnection, messageContext);
094                    }
095                    catch (Exception e) {
096                            throw AxisFault.makeFault(e);
097                    }
098                    finally {
099                            Authenticator.setDefault(null);
100                    }
101            }
102    
103            private void _readFromConnection(
104                            URLConnection urlConnection, MessageContext messageContext)
105                    throws Exception {
106    
107                    HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection;
108    
109                    InputStream inputStream = httpURLConnection.getErrorStream();
110    
111                    if (inputStream == null) {
112                            inputStream = urlConnection.getInputStream();
113                    }
114    
115                    inputStream = new UnsyncBufferedInputStream(inputStream, 8192);
116    
117                    String contentType = urlConnection.getContentType();
118                    String contentLocation = urlConnection.getHeaderField(
119                            "Content-Location");
120    
121                    Message message = new Message(
122                            inputStream, false, contentType, contentLocation);
123    
124                    message.setMessageType(Message.RESPONSE);
125    
126                    messageContext.setResponseMessage(message);
127            }
128    
129            private void _writeToConnection(
130                            URLConnection urlConnection, MessageContext messageContext)
131                    throws Exception {
132    
133                    urlConnection.setDoOutput(true);
134    
135                    Message message = messageContext.getRequestMessage();
136    
137                    String contentType = message.getContentType(
138                            messageContext.getSOAPConstants());
139    
140                    urlConnection.setRequestProperty("Content-Type", contentType);
141    
142                    if (messageContext.useSOAPAction()) {
143                            urlConnection.setRequestProperty(
144                                    "SOAPAction", messageContext.getSOAPActionURI());
145                    }
146    
147                    OutputStream outputStream = new UnsyncBufferedOutputStream(
148                            urlConnection.getOutputStream(), 8192);
149    
150                    message.writeTo(outputStream);
151    
152                    outputStream.flush();
153            }
154    
155            private static Log _log = LogFactoryUtil.getLog(SimpleHTTPSender.class);
156    
157            private Pattern _pattern;
158    
159    }