001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.web.resources;
018
019 import java.net.URI;
020 import javax.ws.rs.Consumes;
021 import javax.ws.rs.GET;
022 import javax.ws.rs.POST;
023 import javax.ws.rs.Produces;
024 import javax.ws.rs.core.MediaType;
025 import javax.ws.rs.core.Response;
026
027 import com.sun.jersey.api.representation.Form;
028 import org.apache.camel.CamelContext;
029 import org.apache.camel.ServiceStatus;
030 import org.apache.camel.model.RouteDefinition;
031
032 /**
033 * Represents the status of a single single Camel Route which is used to implement one or more
034 * <a href="http://camel.apache.org/enterprise-integration-patterns.html">Enterprise Integration Paterns</a>
035 *
036 * @version $Revision: 3365 $
037 */
038 public class RouteStatusResource {
039 private RouteResource routeResource;
040
041 public RouteStatusResource(RouteResource routeResource) {
042 this.routeResource = routeResource;
043 }
044
045 public RouteDefinition getRoute() {
046 return routeResource.getRoute();
047 }
048
049 public CamelContext getCamelContext() {
050 return routeResource.getCamelContext();
051 }
052
053 @GET
054 @Produces(MediaType.TEXT_PLAIN)
055 public String getStatusText() {
056 ServiceStatus status = getStatus();
057 if (status != null) {
058 return status.toString();
059 }
060 return null;
061 }
062
063 public ServiceStatus getStatus() {
064 return getRoute().getStatus();
065 }
066
067 @POST
068 @Consumes(MediaType.TEXT_PLAIN)
069 public Response setStatus(String status) throws Exception {
070 if (status != null) {
071 if (status.equalsIgnoreCase("start")) {
072 getCamelContext().startRoute(getRoute());
073 return Response.ok().build();
074 } else if (status.equalsIgnoreCase("stop")) {
075 getCamelContext().stopRoute(getRoute());
076 return Response.ok().build();
077 }
078 }
079 return Response.noContent().build();
080 }
081
082
083 /**
084 * Sets the status of this route to either "start" or "stop"
085 *
086 * @param formData is the form data POSTed typically from a HTML form with the <code>status</code> field
087 * set to either "start" or "stop"
088 */
089 @POST
090 @Consumes("application/x-www-form-urlencoded")
091 public Response setStatus(Form formData) throws Exception {
092 // TODO replace the Form class with an injected bean?
093 System.out.println("Received form! " + formData);
094 String status = formData.getFirst("status", String.class);
095 setStatus(status);
096 return Response.seeOther(new URI("/routes")).build();
097 }
098
099 }