View Javadoc

1   /*
2    * $Id: AxisDataServer.java,v 1.7 2004/11/03 00:17:56 mch Exp $
3    *
4    * (C) Copyright Astrogrid...
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           //shouldn't happen
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           //throw error if two different calls are setting it to different things
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 $Log: AxisDataServer.java,v $
134 Revision 1.7  2004/11/03 00:17:56  mch
135 PAL_MCH Candidate 2 merge
136 
137 Revision 1.6.8.1  2004/11/02 19:41:26  mch
138 Split TargetIndicator to indicator and maker
139 
140 Revision 1.6  2004/10/08 15:16:04  mch
141 More on providing status
142 
143 Revision 1.5  2004/10/06 11:35:35  mch
144 A bit of tidying up around the web service interfaces.First stage SkyNode implementation
145 
146 Revision 1.4  2004/10/05 19:23:07  mch
147 Added Kevin's url getter
148 
149 Revision 1.3  2004/10/05 14:56:45  mch
150 Added new web interface and partial skynode
151 
152 Revision 1.2  2004/10/01 18:04:59  mch
153 Some factoring out of status stuff, added monitor page
154 
155 Revision 1.1  2004/09/28 15:02:13  mch
156 Merged PAL and server packages
157 
158 Revision 1.46  2004/09/06 20:23:00  mch
159 Replaced metadata generators/servers with plugin mechanism. Added Authority plugin
160 
161 Revision 1.45  2004/08/25 23:38:34  mch
162 (Days changes) moved many query- and results- related classes, renamed packages, added tests, added CIRCLE to sql/adql parsers
163 
164 Revision 1.44  2004/08/18 22:29:21  mch
165 Take more general TargetIndicator rather than AGSL
166 
167 Revision 1.43  2004/08/18 18:44:12  mch
168 Created metadata plugin service and added helper methods
169 
170 Revision 1.42  2004/08/17 20:19:36  mch
171 Moved TargetIndicator to client
172 
173 Revision 1.41  2004/03/18 20:43:07  mch
174 Context test cpde
175 
176 Revision 1.40  2004/03/17 00:27:21  mch
177 Added v05 AxisDataServer
178 
179 Revision 1.39  2004/03/15 17:12:28  mch
180 Added memory to status info
181 
182 Revision 1.38  2004/03/14 04:13:04  mch
183 Wrapped output target in TargetIndicator
184 
185 Revision 1.37  2004/03/14 00:39:55  mch
186 Added error trapping to DataServer and setting Querier error status
187 
188 Revision 1.36  2004/03/13 23:38:46  mch
189 Test fixes and better front-end JSP access
190 
191 Revision 1.35  2004/03/12 20:04:57  mch
192 It05 Refactor (Client)
193 
194 Revision 1.34  2004/03/12 04:45:26  mch
195 It05 MCH Refactor
196 
197 Revision 1.33  2004/03/08 15:57:42  mch
198 Fixes to ensure old ADQL interface works alongside new one and with old plugins
199 
200 Revision 1.32  2004/03/08 00:39:02  mch
201 Minor error message change
202 
203 Revision 1.31  2004/03/08 00:31:28  mch
204 Split out webservice implementations for versioning
205 
206 Revision 1.30  2004/03/07 00:33:50  mch
207 Started to separate It4.1 interface from general server services
208 
209  */
210