1
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.StringPool;
29 import com.liferay.util.SystemProperties;
30
31 import java.io.BufferedInputStream;
32 import java.io.BufferedOutputStream;
33 import java.io.InputStream;
34 import java.io.OutputStream;
35
36 import java.net.Authenticator;
37 import java.net.HttpURLConnection;
38 import java.net.URL;
39 import java.net.URLConnection;
40
41 import java.util.regex.Pattern;
42
43 import org.apache.axis.AxisFault;
44 import org.apache.axis.Message;
45 import org.apache.axis.MessageContext;
46 import org.apache.axis.transport.http.HTTPConstants;
47 import org.apache.axis.transport.http.HTTPSender;
48
49
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 ThreadLocal<String> _currentCookie =
166 new ThreadLocal<String>() {
167
168 protected String initialValue() {
169 return StringPool.BLANK;
170 }
171 };
172
173 private static Log _log = LogFactoryUtil.getLog(SimpleHTTPSender.class);
174
175 private static Pattern _pattern = Pattern.compile(
176 SystemProperties.get(
177 SimpleHTTPSender.class.getName() + ".regexp.pattern"));
178
179 }