1
14
15 package com.liferay.util.axis;
16
17 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedInputStream;
18 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedOutputStream;
19 import com.liferay.portal.kernel.log.Log;
20 import com.liferay.portal.kernel.log.LogFactoryUtil;
21 import com.liferay.portal.kernel.util.GetterUtil;
22 import com.liferay.portal.kernel.util.InitialThreadLocal;
23 import com.liferay.portal.kernel.util.StringPool;
24 import com.liferay.util.SystemProperties;
25
26 import java.io.InputStream;
27 import java.io.OutputStream;
28
29 import java.net.Authenticator;
30 import java.net.HttpURLConnection;
31 import java.net.URL;
32 import java.net.URLConnection;
33
34 import java.util.regex.Pattern;
35
36 import org.apache.axis.AxisFault;
37 import org.apache.axis.Message;
38 import org.apache.axis.MessageContext;
39 import org.apache.axis.transport.http.HTTPConstants;
40 import org.apache.axis.transport.http.HTTPSender;
41
42
47 public class SimpleHTTPSender extends HTTPSender {
48
49 public static String getCurrentCookie() {
50 return _currentCookie.get();
51 }
52
53 public void invoke(MessageContext ctx) throws AxisFault {
54 String url = ctx.getStrProp(MessageContext.TRANS_URL);
55
56 if (_pattern.matcher(url).matches()) {
57 if (_log.isDebugEnabled()) {
58 _log.debug("A match was found for " + url);
59 }
60
61 _invoke(ctx, url);
62 }
63 else {
64 if (_log.isDebugEnabled()) {
65 _log.debug("No match was found for " + url);
66 }
67
68 super.invoke(ctx);
69
70 _registerCurrentCookie(ctx);
71 }
72 }
73
74 private void _invoke(MessageContext ctx, String url) throws AxisFault {
75 try {
76 String userName = ctx.getUsername();
77 String password = ctx.getPassword();
78
79 if ((userName != null) && (password != null)) {
80 Authenticator.setDefault(
81 new SimpleAuthenticator(userName, password));
82 }
83
84 URL urlObj = new URL(url);
85
86 URLConnection urlc = urlObj.openConnection();
87
88 _writeToConnection(urlc, ctx);
89 _readFromConnection(urlc, ctx);
90 }
91 catch (Exception e) {
92 throw AxisFault.makeFault(e);
93 }
94 finally {
95 Authenticator.setDefault(null);
96 }
97 }
98
99 private void _readFromConnection(URLConnection urlc, MessageContext ctx)
100 throws Exception {
101
102 String contentType = urlc.getContentType();
103 String contentLocation = urlc.getHeaderField("Content-Location");
104
105 InputStream is = ((HttpURLConnection)urlc).getErrorStream();
106
107 if (is == null) {
108 is = urlc.getInputStream();
109 }
110
111 is = new UnsyncBufferedInputStream(is, 8192);
112
113 Message response = new Message(is, false, contentType, contentLocation);
114
115 response.setMessageType(Message.RESPONSE);
116
117 ctx.setResponseMessage(response);
118 }
119
120 private void _registerCurrentCookie(MessageContext ctx) {
121 String cookie = StringPool.BLANK;
122
123 try {
124 cookie = GetterUtil.getString(
125 ctx.getStrProp(HTTPConstants.HEADER_COOKIE));
126 }
127 catch (Throwable t) {
128 _log.warn(t);
129 }
130
131 _currentCookie.set(cookie);
132 }
133
134 private void _writeToConnection(URLConnection urlc, MessageContext ctx)
135 throws Exception {
136
137 urlc.setDoOutput(true);
138
139 Message request = ctx.getRequestMessage();
140
141 String contentType = request.getContentType(ctx.getSOAPConstants());
142
143 urlc.setRequestProperty("Content-Type", contentType);
144
145 if (ctx.useSOAPAction()) {
146 urlc.setRequestProperty("SOAPAction", ctx.getSOAPActionURI());
147 }
148
149 OutputStream os = new UnsyncBufferedOutputStream(
150 urlc.getOutputStream(), 8192);
151
152 request.writeTo(os);
153
154 os.flush();
155 }
156
157 private static Log _log = LogFactoryUtil.getLog(SimpleHTTPSender.class);
158
159 private static ThreadLocal<String> _currentCookie =
160 new InitialThreadLocal<String>(StringPool.BLANK);
161 private static Pattern _pattern = Pattern.compile(
162 SystemProperties.get(
163 SimpleHTTPSender.class.getName() + ".regexp.pattern"));
164
165 }