1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util.axis;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.util.SystemProperties;
27  
28  import java.io.BufferedInputStream;
29  import java.io.BufferedOutputStream;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  
33  import java.net.Authenticator;
34  import java.net.HttpURLConnection;
35  import java.net.URL;
36  import java.net.URLConnection;
37  
38  import java.util.regex.Pattern;
39  
40  import org.apache.axis.AxisFault;
41  import org.apache.axis.Message;
42  import org.apache.axis.MessageContext;
43  import org.apache.axis.transport.http.HTTPConstants;
44  import org.apache.axis.transport.http.HTTPSender;
45  
46  /**
47   * <a href="SimpleHTTPSender.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class SimpleHTTPSender extends HTTPSender {
53  
54      public static String getCurrentCookie() {
55          return _currentCookie.get();
56      }
57  
58      public void invoke(MessageContext ctx) throws AxisFault {
59          String url = ctx.getStrProp(MessageContext.TRANS_URL);
60  
61          if (_pattern.matcher(url).matches()) {
62              if (_log.isDebugEnabled()) {
63                  _log.debug("A match was found for " + url);
64              }
65  
66              _invoke(ctx, url);
67          }
68          else {
69              if (_log.isDebugEnabled()) {
70                  _log.debug("No match was found for " + url);
71              }
72  
73              super.invoke(ctx);
74  
75              _registerCurrentCookie(ctx);
76          }
77      }
78  
79      private void _invoke(MessageContext ctx, String url) throws AxisFault {
80          try {
81              String userName = ctx.getUsername();
82              String password = ctx.getPassword();
83  
84              if ((userName != null) && (password != null)) {
85                  Authenticator.setDefault(
86                      new SimpleAuthenticator(userName, password));
87              }
88  
89              URL urlObj = new URL(url);
90  
91              URLConnection urlc = urlObj.openConnection();
92  
93              _writeToConnection(urlc, ctx);
94              _readFromConnection(urlc, ctx);
95          }
96          catch (Exception e) {
97              throw AxisFault.makeFault(e);
98          }
99          finally {
100             Authenticator.setDefault(null);
101         }
102     }
103 
104     private void _readFromConnection(URLConnection urlc, MessageContext ctx)
105         throws Exception {
106 
107         String contentType = urlc.getContentType();
108         String contentLocation = urlc.getHeaderField("Content-Location");
109 
110         InputStream is = ((HttpURLConnection) urlc).getErrorStream();
111 
112         if (is == null) {
113             is = urlc.getInputStream();
114         }
115 
116         is = new BufferedInputStream(is, 8192);
117 
118         Message response = new Message(is, false, contentType, contentLocation);
119 
120         response.setMessageType(Message.RESPONSE);
121 
122         ctx.setResponseMessage(response);
123     }
124 
125     private void _registerCurrentCookie(MessageContext ctx) {
126         String cookie = StringPool.BLANK;
127 
128         try {
129             cookie = GetterUtil.getString(
130                 ctx.getStrProp(HTTPConstants.HEADER_COOKIE));
131         }
132         catch (Throwable t) {
133             _log.warn(t);
134         }
135 
136         _currentCookie.set(cookie);
137     }
138 
139     private void _writeToConnection(URLConnection urlc, MessageContext ctx)
140         throws Exception {
141 
142         urlc.setDoOutput(true);
143 
144         Message request = ctx.getRequestMessage();
145 
146         String contentType = request.getContentType(ctx.getSOAPConstants());
147 
148         urlc.setRequestProperty("Content-Type", contentType);
149 
150         if (ctx.useSOAPAction()) {
151             urlc.setRequestProperty("SOAPAction", ctx.getSOAPActionURI());
152         }
153 
154         OutputStream os = new BufferedOutputStream(
155             urlc.getOutputStream(), 8192);
156 
157         request.writeTo(os);
158 
159         os.flush();
160     }
161 
162     private static ThreadLocal<String> _currentCookie =
163         new ThreadLocal<String>() {
164 
165         protected String initialValue() {
166             return StringPool.BLANK;
167         }
168     };
169 
170     private static Log _log = LogFactoryUtil.getLog(SimpleHTTPSender.class);
171 
172     private static Pattern _pattern = Pattern.compile(
173         SystemProperties.get(
174             SimpleHTTPSender.class.getName() + ".regexp.pattern"));
175 
176 }