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