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