org.benow.service.http
Class HttpServiceRequestHandler
java.lang.Object
org.benow.web.servlet.RequestHandler
org.benow.web.servlet.SecureHandler
org.benow.service.http.HttpServiceRequestHandler
- All Implemented Interfaces:
- java.lang.Runnable
public class HttpServiceRequestHandler
- extends SecureHandler
Parses request url and invokes a service method, returning the result of invocation. Provides an easy way
to address services via an http request.
Invocation urls are in the form: /svc/fully.qualified.ServiceName and submission is in post or get form
with the following parameters:
- exec, signature of method to invoke within the service. ie doSomething(String) or
doSomething(java.lang.String).
- method params, the parameters for the method to invoke
- Basic params in paramName=value form. If, for example the declaration of doSomething was
public SomeObject doSomething(String withParam);
then a withParam='input string' parameter should be specified.
Basic type conversion is supported, so ints, longs, doubles, etc are automatically converted from their
string representation.
NOTE the parameter names are (currently) introspected from the code via ParamName annotations for method
attributes within the service interface. If no ParamName annotation is given, the params are named param0,
param1, etc according to the annotation index.
- Complex types. Types can be specified by a combination of parameters. if, for example, a method was
declared with a complex type:
public SomeObject doSomething(ComplexObject withObj);
the parameters starting with withObj_ are used to construct the ComplexObject.
withObj_type=fqcn (optional) specifies the fully qualified class name to construct. If not given, the
declaration type (ie ComplexType) in used. Classes are instantiated using a zero parameter constructor, so
one must be present (but may be private or protected).
withObj_field1=valueOfField1 specified the value for field1 which is populated in the instance. Type rules
for the field are similar to those for method params (ie auto coversion or complex type/value
specification).
- Repository values can be specified. If the method parameter is a persistent object it can be fetched
and used if the key and is specified:
withObj_key=1234 key of object of given type to fetch from repository.
Object is fetched and used in method invocation. withObj_type=fqcn (optional), repository class to fetch,
if not given, declaration class is used.
- redirection. The default return mechanism is to serialize the method result to xml. If
redirection is to occur after invocation, it can be specified via a
redirect parameter.
Special symbols may be present in the redirect url as follows:
- [result/fieldName]: substituted with the value of fieldName within the returned object
- [params/paramName]: substituted with the value of the named parameter
- [session/attrName]: substituted with the value of named attribute in session
- [ref]: the base referer url (stripped of any params).
If, for example, the request came from http://somehost.com/some/thing.page?op=create to
http://anotherhost.com/svc/some.SomethingService with a redirect of
[ref]?op=view&name=[result/name] the redirected url would be
http://somehost.com/some/thing.page?op=view&name=someName if someName was the value of the
name within the returned object.
- exception handling. A redirect can happen on exceptions, if specified. The method for specifying
redirect urls in via onException=url parameters. For example
onIOException=/errHandler.page
would redirect to /errHandler.page if an IOException occured. The exception class name is a simple name,
unless two exceptions with the same simple name are thrown by the method (bad practice)
This handler allows for much flexibility for http method invocation and result delivery. Due to the use of
standard http, it also allows for many differing types of clients to connect. It is also the default
connection method for remote invocation used in the Service API.
Additionally, when accessed via a web browser, a javadoc-like display of services and methods is displayed.
In such a way development against services is facilitated.
- Author:
- andy
- See Also:
org.benow.java.spec.ParamName,
HttpServiceMethod
|
Method Summary |
static boolean |
canEditService(java.lang.Class<?> serviceClass)
|
long |
copy(java.io.InputStream input,
java.io.OutputStream out)
|
protected java.lang.Object |
doRun(java.io.OutputStream out)
if it's a collection we need to circumvent castor and handle the marshalling/unmarshalling ourselves
(castor has issues with marshalling/unmarshalling lists) the method is in this class so as to eliminate
multi-threading issues. |
protected void |
dumpError(java.io.PrintWriter out,
int code,
java.lang.String message,
java.lang.Throwable t)
handles exceptions. |
static java.lang.String |
getRedirectToCreateURL(java.lang.String failMsg,
java.lang.Class<?> serviceClass,
java.lang.String methodSig)
create url to create method page. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
HttpServiceRequestHandler
public HttpServiceRequestHandler(HandlerServlet servlet)
dumpError
protected void dumpError(java.io.PrintWriter out,
int code,
java.lang.String message,
java.lang.Throwable t)
throws java.io.IOException,
RedirectException
- handles exceptions. If a referrer is given and there is a onorg.pkg.SomeException, onSomeException or
onSome parameter (where SomeException is the type of the exception being handled) with a value, a
redirect is given to a handler url as specified in the value (relative to the referrer). If there is no
referrer or no exception displayer defined, an xml exception dump is given.
- Overrides:
dumpError in class RequestHandler
- Parameters:
response - t - out -
- Throws:
java.io.IOException
RedirectException
javax.xml.transform.TransformerException
doRun
protected java.lang.Object doRun(java.io.OutputStream out)
throws java.lang.Exception
- if it's a collection we need to circumvent castor and handle the marshalling/unmarshalling ourselves
(castor has issues with marshalling/unmarshalling lists) the method is in this class so as to eliminate
multi-threading issues.
- Overrides:
doRun in class RequestHandler
- Parameters:
coll - out -
- Throws:
javax.xml.transform.TransformerException
org.benow.java.mapping.MappingException
java.io.IOException - void marshallCollection(Collection extends Object> coll, PrintWriter out) throws TransformerException {
out.println("");
for (Iterator> i = coll.iterator(); i.hasNext();) {
Object curr = i.next();
out.print(" - ");
if (String.class.equals(curr.getClass()))
out.print(curr);
else if (Class.class.equals(curr.getClass())) {
out.print(((Class) curr).getName());
} else {
out.println();
ClassTransformer x = new ClassTransformer(curr);
OutputFormat of = x.createDefaultOutputFormat();
of.setOmitXMLDeclaration(true);
x.to(out, of);
out.print(" ");
}
out.println("
");
}
out.println("
");
}
java.lang.Exception- See Also:
HttpServiceMethod.unmarshallCollection(Class, String)
canEditService
public static boolean canEditService(java.lang.Class<?> serviceClass)
- Parameters:
serviceClass -
- Returns:
- true if current user has service edit permission
getRedirectToCreateURL
public static java.lang.String getRedirectToCreateURL(java.lang.String failMsg,
java.lang.Class<?> serviceClass,
java.lang.String methodSig)
- create url to create method page. Call when no service method. Introspects call to smartly generate
method sig
- Parameters:
failMsg - serviceClass - methodSig -
- Returns:
copy
public long copy(java.io.InputStream input,
java.io.OutputStream out)
throws java.io.IOException
- Throws:
java.io.IOException