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.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
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 }