1
2
3
4
5
6
7 package org.astrogrid.dataservice.service;
8
9 import java.io.PrintWriter;
10 import java.io.StringWriter;
11 import java.net.MalformedURLException;
12 import java.net.URL;
13 import java.security.Principal;
14 import javax.servlet.http.HttpServletRequest;
15 import org.apache.axis.AxisEngine;
16 import org.apache.axis.AxisFault;
17 import org.apache.axis.MessageContext;
18 import org.apache.axis.server.AxisServer;
19 import org.apache.axis.transport.http.HTTPConstants;
20 import org.astrogrid.io.account.LoginAccount;
21
22 /***
23 * A class for serving data through an Axis webservice implementation. Implementations
24 * are the Axis interface classes and should 'have' it rather than 'subclass' it; if
25 * they 'subclass' it then this classes methods get exposed
26 * <p>
27 * It can be a singleton; state comes from the Queriers.
28
29 * @author M Hill
30 * @author Noel Winstanly
31 *
32 */
33
34 public class AxisDataServer extends DataServer {
35
36 /*** Constant for makeFault - input from client has caused problem */
37 public final static boolean CLIENTFAULT = true;
38 /*** Constant for makeFault - problem with server (or unknown) */
39 public final static boolean SERVERFAULT = false;
40
41
42 /*** Works out the url stem from the Message axis context */
43 public static String getMessageContextUrlStem() {
44 MessageContext context = MessageContext.getCurrentContext();
45 if (context == null) {
46 return null;
47 }
48 HttpServletRequest req = (HttpServletRequest) context.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
49 try {
50 log.debug("AxisDataServer server "+req.getServerName()+":"+req.getServerPort()+"/"+req.getContextPath());
51 URL server = new URL("http", req.getServerName(), req.getServerPort(), req.getContextPath());
52
53 return server.toString();
54 }
55 catch (MalformedURLException e) {
56
57 log.error(e+" getting url from HttpServletRequest",e);
58 throw new RuntimeException("URL from HttpServletRequest was malformed??! (AxisDataService_v05): ",e);
59 }
60 }
61
62 public String getContext() {
63 try {
64 AxisEngine engine = AxisServer.getServer(null);
65 return engine.getApplicationSession().toString();
66 } catch (AxisFault af) {
67 log.error("Getting application context",af);
68 return null;
69 }
70 }
71
72 /*** Returns some ID of the client making the call to this axis service */
73 public static String getSource() {
74 MessageContext context = MessageContext.getCurrentContext();
75
76 if (context != null) {
77
78 HttpServletRequest request = (HttpServletRequest) context.getProperty(org.apache.axis.transport.http.HTTPConstants.MC_HTTP_SERVLETREQUEST);
79
80
81 return request.getRemoteHost();
82 }
83 return "";
84 }
85
86 /*** Returns user given in SOAP message */
87 public Principal getUser() {
88 if (MessageContext.getCurrentContext() != null) {
89 String username = MessageContext.getCurrentContext().getUsername();
90 if ((username != null) && (username.trim().length() >0)) {
91 return new LoginAccount(username, MessageContext.getCurrentContext().getPassword());
92 }
93 }
94
95 return LoginAccount.ANONYMOUS;
96 }
97
98 /***
99 * Axis provides an AxisFault for reporting errors through SOAP. This method
100 * creates a fault from a message and a cause, and includes in the detail
101 * the cause's (relevent) stack trace
102 * @blameClient - true if the error is known to be caused by an input parameter - such as an
103 * invalid query ID.
104 */
105 public AxisFault makeFault(boolean blameClient, String message, Throwable cause) {
106
107 log.error(message+" [...will 'throw' AxisFault '"+cause+"' to client]", cause);
108
109 AxisFault fault = new AxisFault(message);
110
111
112
113
114
115
116
117
118
119 fault.clearFaultDetails();
120 while (cause != null) {
121 StringWriter writer = new StringWriter();
122 cause.printStackTrace(new PrintWriter(writer));
123 fault.addFaultDetailString(writer.toString());
124 cause = cause.getCause();
125 }
126
127 return fault;
128 }
129
130 /***
131 * Convenience method to generate server error
132 */
133 public AxisFault makeFault(String message) {
134 return makeFault(SERVERFAULT, message, null);
135 }
136
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244