001    /**
002     * Copyright (c) 2000-present 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                    else {
053                            _pattern = null;
054                    }
055            }
056    
057            @Override
058            public void invoke(MessageContext messageContext) throws AxisFault {
059                    String url = messageContext.getStrProp(MessageContext.TRANS_URL);
060    
061                    Matcher matcher = null;
062    
063                    if (_pattern != null) {
064                            matcher = _pattern.matcher(url);
065                    }
066    
067                    if ((matcher != null) && matcher.matches()) {
068                            if (_log.isDebugEnabled()) {
069                                    _log.debug("A match was found for " + url);
070                            }
071    
072                            _invoke(messageContext, url);
073                    }
074                    else {
075                            super.invoke(messageContext);
076                    }
077            }
078    
079            private void _invoke(MessageContext messageContext, String url)
080                    throws AxisFault {
081    
082                    try {
083                            String userName = messageContext.getUsername();
084                            String password = messageContext.getPassword();
085    
086                            if ((userName != null) && (password != null)) {
087                                    Authenticator.setDefault(
088                                            new SimpleAuthenticator(userName, password));
089                            }
090    
091                            URL urlObj = new URL(url);
092    
093                            URLConnection urlConnection = urlObj.openConnection();
094    
095                            _writeToConnection(urlConnection, messageContext);
096                            _readFromConnection(urlConnection, messageContext);
097                    }
098                    catch (Exception e) {
099                            throw AxisFault.makeFault(e);
100                    }
101                    finally {
102                            Authenticator.setDefault(null);
103                    }
104            }
105    
106            private void _readFromConnection(
107                            URLConnection urlConnection, MessageContext messageContext)
108                    throws Exception {
109    
110                    HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection;
111    
112                    InputStream inputStream = httpURLConnection.getErrorStream();
113    
114                    if (inputStream == null) {
115                            inputStream = urlConnection.getInputStream();
116                    }
117    
118                    inputStream = new UnsyncBufferedInputStream(inputStream, 8192);
119    
120                    String contentType = urlConnection.getContentType();
121                    String contentLocation = urlConnection.getHeaderField(
122                            "Content-Location");
123    
124                    Message message = new Message(
125                            inputStream, false, contentType, contentLocation);
126    
127                    message.setMessageType(Message.RESPONSE);
128    
129                    messageContext.setResponseMessage(message);
130            }
131    
132            private void _writeToConnection(
133                            URLConnection urlConnection, MessageContext messageContext)
134                    throws Exception {
135    
136                    urlConnection.setDoOutput(true);
137    
138                    Message message = messageContext.getRequestMessage();
139    
140                    String contentType = message.getContentType(
141                            messageContext.getSOAPConstants());
142    
143                    urlConnection.setRequestProperty("Content-Type", contentType);
144    
145                    if (messageContext.useSOAPAction()) {
146                            urlConnection.setRequestProperty(
147                                    "SOAPAction", messageContext.getSOAPActionURI());
148                    }
149    
150                    OutputStream outputStream = new UnsyncBufferedOutputStream(
151                            urlConnection.getOutputStream(), 8192);
152    
153                    message.writeTo(outputStream);
154    
155                    outputStream.flush();
156            }
157    
158            private static final Log _log = LogFactoryUtil.getLog(
159                    SimpleHTTPSender.class);
160    
161            private final Pattern _pattern;
162    
163    }