View Javadoc

1   /*
2    * $Id: AxisDataServer.java,v 1.2 2005/03/11 15:17:52 mch Exp $
3    *
4    * (C) Copyright Astrogrid...
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.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  //         log.debug("AxisDataServer finds url stem "+server+" in request "+req);
53           return server.toString();
54        }
55        catch (MalformedURLException e) {
56           //shouldn't happen
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           //get http request that contained the message
78           HttpServletRequest request = (HttpServletRequest) context.getProperty(org.apache.axis.transport.http.HTTPConstants.MC_HTTP_SERVLETREQUEST);
79     
80           //return remove address of request
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       /* these fault codes are deprecated
112       if (blameClient) {
113          fault.setFaultCode("Client");
114       } else {
115          fault.setFaultCode("Server");
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 $Log: AxisDataServer.java,v $
141 Revision 1.2  2005/03/11 15:17:52  mch
142 removed calls to deprecated setFault
143 
144 Revision 1.1.1.1  2005/02/17 18:37:35  mch
145 Initial checkin
146 
147 Revision 1.1.1.1  2005/02/16 17:11:24  mch
148 Initial checkin
149 
150 Revision 1.7.12.4  2004/12/07 21:21:09  mch
151 Fixes after a days integration testing
152 
153 Revision 1.7.12.3  2004/12/05 19:33:16  mch
154 changed skynode to 'raw' soap (from axis) and bug fixes
155 
156 Revision 1.7.12.2  2004/11/29 22:33:42  mch
157 added better logging and fix for protocol oops
158 
159 Revision 1.7.12.1  2004/11/29 21:52:18  mch
160 Fixes to skynode, log.error(), getstem, status logger, etc following tests on grendel
161 
162 Revision 1.7  2004/11/03 00:17:56  mch
163 PAL_MCH Candidate 2 merge
164 
165 Revision 1.6.8.1  2004/11/02 19:41:26  mch
166 Split TargetIndicator to indicator and maker
167 
168 Revision 1.6  2004/10/08 15:16:04  mch
169 More on providing status
170 
171 Revision 1.5  2004/10/06 11:35:35  mch
172 A bit of tidying up around the web service interfaces.First stage SkyNode implementation
173 
174 Revision 1.4  2004/10/05 19:23:07  mch
175 Added Kevin's url getter
176 
177 Revision 1.3  2004/10/05 14:56:45  mch
178 Added new web interface and partial skynode
179 
180 Revision 1.2  2004/10/01 18:04:59  mch
181 Some factoring out of status stuff, added monitor page
182 
183 Revision 1.1  2004/09/28 15:02:13  mch
184 Merged PAL and server packages
185 
186 Revision 1.46  2004/09/06 20:23:00  mch
187 Replaced metadata generators/servers with plugin mechanism. Added Authority plugin
188 
189 Revision 1.45  2004/08/25 23:38:34  mch
190 (Days changes) moved many query- and results- related classes, renamed packages, added tests, added CIRCLE to sql/adql parsers
191 
192 Revision 1.44  2004/08/18 22:29:21  mch
193 Take more general TargetIndicator rather than AGSL
194 
195 Revision 1.43  2004/08/18 18:44:12  mch
196 Created metadata plugin service and added helper methods
197 
198 Revision 1.42  2004/08/17 20:19:36  mch
199 Moved TargetIndicator to client
200 
201 Revision 1.41  2004/03/18 20:43:07  mch
202 Context test cpde
203 
204 Revision 1.40  2004/03/17 00:27:21  mch
205 Added v05 AxisDataServer
206 
207 Revision 1.39  2004/03/15 17:12:28  mch
208 Added memory to status info
209 
210 Revision 1.38  2004/03/14 04:13:04  mch
211 Wrapped output target in TargetIndicator
212 
213 Revision 1.37  2004/03/14 00:39:55  mch
214 Added error trapping to DataServer and setting Querier error status
215 
216 Revision 1.36  2004/03/13 23:38:46  mch
217 Test fixes and better front-end JSP access
218 
219 Revision 1.35  2004/03/12 20:04:57  mch
220 It05 Refactor (Client)
221 
222 Revision 1.34  2004/03/12 04:45:26  mch
223 It05 MCH Refactor
224 
225 Revision 1.33  2004/03/08 15:57:42  mch
226 Fixes to ensure old ADQL interface works alongside new one and with old plugins
227 
228 Revision 1.32  2004/03/08 00:39:02  mch
229 Minor error message change
230 
231 Revision 1.31  2004/03/08 00:31:28  mch
232 Split out webservice implementations for versioning
233 
234 Revision 1.30  2004/03/07 00:33:50  mch
235 Started to separate It4.1 interface from general server services
236 
237  */
238