View Javadoc

1   package org.srinivas.siteworks.rlrestserver;
2   
3   import org.apache.camel.Exchange;
4   import org.apache.camel.Processor;
5   import org.apache.camel.builder.RouteBuilder;
6   import org.apache.camel.model.rest.RestBindingMode;
7   import org.restlet.data.Header;
8   import org.restlet.data.MediaType;
9   import org.restlet.engine.header.HeaderConstants;
10  import org.restlet.util.Series;
11  
12  /**
13   * The Class RLRestServerRoute.
14   */
15  public class RLRestServerRoute extends RouteBuilder {
16  	public static final String CAMEL_ENDPOINT_DIRECT_START = "direct:start";
17  	private static final String CAMEL_ENDPOINT_DIRECT_RLFIREWORKSCOLLECTION = "direct:RLfireworkscollection";
18  	public static final String CAMEL_ENDPOINT_DIRECT_RLUPDATEFIREWORK = "direct:RLupdatefirework";
19  	public static final String CAMEL_ENDPOINT_DIRECT_RLRESPONSEPROCESS = "direct:RLresponseprocess";
20  	public static final String CAMEL_ENDPOINT_DIRECT_ERROR = "direct:error";
21  	public static final int PORT_NUMBER_8180 = 8180;
22  	public static final String DOMAIN_LOCALHOST = "localhost";
23  	public static final String RLRESPONSEPROCESS = "RLresponseprocess";
24  	public static final String RLUPDATEFIREWORK = "RLupdatefirework";
25  	public static final String RLFIREWORKSCOLLECTION = "RLfireworkscollection";
26  	public static final String RL_ROUTE_STATUS = "RLRouteStatus";
27  	
28  	
29  	/* (non-Javadoc)
30  	 * @see org.apache.camel.builder.RouteBuilder#configure()
31  	 */
32  	@Override
33  	public void configure() throws Exception {
34  		onException(Exception.class).onWhen(header(RL_ROUTE_STATUS).contains(RLFIREWORKSCOLLECTION)).handled(true).to(CAMEL_ENDPOINT_DIRECT_ERROR).end();
35  		onException(Exception.class).onWhen(header(RL_ROUTE_STATUS).contains(RLUPDATEFIREWORK)).handled(true).to(CAMEL_ENDPOINT_DIRECT_ERROR).end();
36  		onException(Exception.class).onWhen(header(RL_ROUTE_STATUS).contains(RLRESPONSEPROCESS)).to(CAMEL_ENDPOINT_DIRECT_ERROR).handled(true).end();
37  		onException(Exception.class).handled(true).transform().constant("Sorry RLRestServer Could Not Process Due to Errors").end();
38  
39  		restConfiguration().component("restlet").host(DOMAIN_LOCALHOST).port(PORT_NUMBER_8180)
40  				.bindingMode(RestBindingMode.auto).dataFormatProperty("prettyPrint", "true")
41  				.apiContextPath("/api-doc").apiProperty("api.title", "User API")
42  				.apiProperty("api.version", "1.2.3").apiProperty("cors", "true");
43  
44  		rest("/fireworks/getAll").description("Restlet for Get All FireWorks").get()
45  				.description("Get Request of Restlet for Get All FireWorks and It Produces XML")
46  				.outType(String.class).produces("text/plain").route()
47  				.to(CAMEL_ENDPOINT_DIRECT_RLFIREWORKSCOLLECTION).endRest().post()
48  				.description("POST Request of Restlet for Get All FireWorks and It Produces XML")
49  				.outType(String.class).produces("text/plain").route()
50  				.to(CAMEL_ENDPOINT_DIRECT_RLFIREWORKSCOLLECTION);
51  		rest("/fireworks/updatefirework").description("Restlet for Update Firework").post()
52  				.description("Post Request of Restlet for Update Firework and It Produces XML")
53  				.outType(String.class).produces("text/plain").route().to(CAMEL_ENDPOINT_DIRECT_RLUPDATEFIREWORK);
54  
55  		from(CAMEL_ENDPOINT_DIRECT_START).choice().when(body().isEqualToIgnoreCase("fireworks/getAll"))
56  				.to("restlet:http://localhost:8180/fireworks/getAll?restletMethods=GET")
57  				.when(body().isEqualToIgnoreCase("fireworks/updatefirework"))
58  				.to("restlet:http://localhost:8180/fireworks/updatefirework?restletMethod=POST")
59  				.otherwise().transform(simple("${body}: does not have a corresponding Restlet"))
60  				.end();
61  
62  		from(CAMEL_ENDPOINT_DIRECT_RLFIREWORKSCOLLECTION)
63  				.setHeader(RL_ROUTE_STATUS, constant(RLFIREWORKSCOLLECTION))
64  				.process(new Processor() {
65  					public void process(Exchange exchange) throws Exception {
66  						exchange.getIn().setHeader("org.restlet.http.headers",
67  								new Series<Header>(Header.class));
68  					}
69  				}).to("restlet:http://localhost:8181/fireworks/getAll?restletMethods=GET")
70  				.to(CAMEL_ENDPOINT_DIRECT_RLRESPONSEPROCESS).log("Message ${body}");
71  
72  		from(CAMEL_ENDPOINT_DIRECT_RLUPDATEFIREWORK)
73  				.setHeader(RL_ROUTE_STATUS, constant(RLUPDATEFIREWORK))
74  				.setHeader(HeaderConstants.HEADER_CONTENT_TYPE, constant(MediaType.TEXT_PLAIN))
75  				.transform()
76  				.simple("${in.header[fireworkupdatekey]}")
77  				.process(new Processor() {
78  					public void process(Exchange exchange) throws Exception {
79  						exchange.getIn().setHeader("org.restlet.http.headers",
80  								new Series<Header>(Header.class));
81  					}
82  				}).to("restlet:http://localhost:8181/fireworks/updatefirework?restletMethod=POST")
83  				.log("Message in RLupdatefirework finsished to dbserver")
84  				.to(CAMEL_ENDPOINT_DIRECT_RLRESPONSEPROCESS).log("Message ${body}");
85  
86  		from(CAMEL_ENDPOINT_DIRECT_RLRESPONSEPROCESS).setHeader(RL_ROUTE_STATUS, constant(RLRESPONSEPROCESS))
87  				.log("Message in RLresponseprocess moving to xquery").convertBodyTo(String.class)
88  				.to("xquery:removeNameSpaces.xqy").convertBodyTo(String.class)
89  				.log("Message ${body}").setHeader("sequence")
90  				.groovy("resource:classpath:Mygroovy.groovy");
91  
92  		from(CAMEL_ENDPOINT_DIRECT_ERROR).process(new RLRestServerErrorHandlerProcessor())
93  				.log("Message ${body}");
94  
95  	}
96  
97  }