1
2
3
4
5
6
7 package org.astrogrid.datacenter.service;
8
9 import java.io.PrintWriter;
10 import java.io.StringWriter;
11 import java.net.MalformedURLException;
12 import java.net.URL;
13 import javax.servlet.ServletContext;
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
21 /***
22 * A class for serving data through an Axis webservice implementation. Implementations
23 * are the Axis interface classes and should 'have' it rather than 'subclass' it; if
24 * they 'subclass' it then this classes methods get exposed
25 * <p>
26 * It can be a singleton; state comes from the Queriers.
27
28 * @author M Hill
29 * @author Noel Winstanly
30 *
31 */
32
33 public class AxisDataServer extends DataServer {
34
35 /*** Constant for makeFault - input from client has caused problem */
36 public final static boolean CLIENTFAULT = true;
37 /*** Constant for makeFault - problem with server (or unknown) */
38 public final static boolean SERVERFAULT = false;
39
40 /*** set during init to the url stem for this context, eg http://grendel12.roe.ac.uk/pal-6df" target="alexandria_uri">http://grendel12.roe.ac.uk/pal-6df */
41 protected static String contextUrlStem = null;
42
43
44 /*** Returns the url stem for this axis context, eg http://grendel12.roe.ac.uk/pal-6df" target="alexandria_uri">http://grendel12.roe.ac.uk/pal-6df */
45 public static String getUrlStem() {
46 if (contextUrlStem != null) {
47 return contextUrlStem;
48 }
49 String axisStem = getAxisUrlStem();
50 if (axisStem != null) {
51 setUrlStem(axisStem);
52 return contextUrlStem;
53 }
54 return null;
55 }
56
57 /*** Works out the url stem from the Message axis context */
58 public static String getAxisUrlStem() {
59 MessageContext context = MessageContext.getCurrentContext();
60 if (context == null) {
61 return null;
62 }
63 HttpServletRequest req = (HttpServletRequest) context.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
64 try {
65 URL server = new URL(req.getProtocol(), req.getServerName(), req.getServerPort(), req.getContextPath());
66 return server.toString();
67 }
68 catch (MalformedURLException e) {
69
70 throw new RuntimeException("Http Request URL malformed: "+e);
71 }
72 }
73
74 public static void setUrlStem(String stem) {
75 if (contextUrlStem == null) {
76 contextUrlStem = stem;
77 }
78 else if (!contextUrlStem.equals(stem)) {
79
80 throw new IllegalArgumentException("Setting url stem to '"+stem+"' but it's already set to '"+contextUrlStem+"'");
81 }
82 }
83
84 public String getContext() {
85 try {
86 AxisEngine engine = AxisServer.getServer(null);
87 return engine.getApplicationSession().toString();
88 } catch (AxisFault af) {
89 log.error("Getting application context",af);
90 return null;
91 }
92 }
93
94 /***
95 * Axis provides an AxisFault for reporting errors through SOAP. This method
96 * creates a fault from a message and a cause, and includes in the detail
97 * the cause's (relevent) stack trace
98 * @blameClient - true if the error is known to be caused by an input parameter - such as an
99 * invalid query ID.
100 */
101 public AxisFault makeFault(boolean blameClient, String message, Throwable cause) {
102
103 log.error("AxisFault being generated: 'Throwing' exception "+cause+" to client, message="+message, cause);
104
105 AxisFault fault = new AxisFault(message);
106
107 if (blameClient) {
108 fault.setFaultCode("Client");
109 } else {
110 fault.setFaultCode("Server");
111 }
112
113 fault.clearFaultDetails();
114 if (cause != null) {
115 StringWriter writer = new StringWriter();
116 cause.printStackTrace(new PrintWriter(writer));
117 fault.addFaultDetailString(writer.toString());
118 }
119
120 return fault;
121 }
122
123 /***
124 * Convenience method to generate server error
125 */
126 public AxisFault makeFault(String message) {
127 return makeFault(SERVERFAULT, message, null);
128 }
129
130 }
131
132
133
134
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