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