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