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