You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2009/02/18 16:57:41 UTC

svn commit: r745547 - in /camel/trunk/components/camel-web/src/main: java/org/apache/camel/rest/model/ java/org/apache/camel/rest/resources/ webapp/WEB-INF/decorators/ webapp/css/ webapp/org/apache/camel/rest/resources/CamelContextResource/ webapp/org/...

Author: jstrachan
Date: Wed Feb 18 15:57:40 2009
New Revision: 745547

URL: http://svn.apache.org/viewvc?rev=745547&view=rev
Log:
allow endpoints to be created; with errors reported back to the user if they try to create an unknown endpoint. Also introduced EndpointsResource to make the root resource a bit less fat - and also to allow stateful fields to be included; like the error message or current URI - for re-rendering the view if a post fails the validation rules

Added:
    camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/resources/EndpointsResource.java   (with props)
    camel/trunk/components/camel-web/src/main/webapp/org/apache/camel/rest/resources/EndpointsResource/
    camel/trunk/components/camel-web/src/main/webapp/org/apache/camel/rest/resources/EndpointsResource/index.jsp   (contents, props changed)
      - copied, changed from r745452, camel/trunk/components/camel-web/src/main/webapp/org/apache/camel/rest/resources/CamelContextResource/endpoints.jsp
Removed:
    camel/trunk/components/camel-web/src/main/webapp/org/apache/camel/rest/resources/CamelContextResource/endpoints.jsp
    camel/trunk/components/camel-web/src/main/webapp/org/apache/camel/rest/resources/CamelContextResource/foo.jsp
Modified:
    camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/model/EndpointLink.java
    camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/resources/CamelContextResource.java
    camel/trunk/components/camel-web/src/main/webapp/WEB-INF/decorators/main.jsp
    camel/trunk/components/camel-web/src/main/webapp/css/type-settings.css

Modified: camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/model/EndpointLink.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/model/EndpointLink.java?rev=745547&r1=745546&r2=745547&view=diff
==============================================================================
--- camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/model/EndpointLink.java (original)
+++ camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/model/EndpointLink.java Wed Feb 18 15:57:40 2009
@@ -70,6 +70,6 @@
 
     protected String createHref(String uri) {
         // TODO how to encode as a href?
-        return "/endpoint/" + uri;
+        return "/endpoints/" + uri;
     }
 }

Modified: camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/resources/CamelContextResource.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/resources/CamelContextResource.java?rev=745547&r1=745546&r2=745547&view=diff
==============================================================================
--- camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/resources/CamelContextResource.java (original)
+++ camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/resources/CamelContextResource.java Wed Feb 18 15:57:40 2009
@@ -17,8 +17,8 @@
 package org.apache.camel.rest.resources;
 
 
+import com.sun.jersey.api.representation.Form;
 import com.sun.jersey.api.view.ImplicitProduces;
-import com.sun.jersey.api.spring.Autowire;
 import com.sun.jersey.spi.inject.Inject;
 import com.sun.jersey.spi.resource.Singleton;
 import org.apache.camel.CamelContext;
@@ -29,15 +29,20 @@
 import org.apache.camel.rest.model.Camel;
 import org.apache.camel.rest.model.EndpointLink;
 import org.apache.camel.rest.model.Endpoints;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
 
+import javax.annotation.PreDestroy;
+import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
+import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
-import javax.annotation.PreDestroy;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.core.Context;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.List;
 
 
@@ -64,6 +69,10 @@
         return camelContext;
     }
 
+    public ProducerTemplate getTemplate() {
+        return template;
+    }
+
     public String getName() {
         return camelContext.getName();
     }
@@ -86,35 +95,13 @@
         return new Camel(camelContext);
     }
 
-
-    /**
-     * Returns a list of endpoints available in this context
-     *
-     * @return
-     */
-    @GET
     @Path("endpoints")
-    @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
-    public Endpoints getEndpointsDTO() {
-        return new Endpoints(camelContext);
+    public EndpointsResource getEndpointsResource() {
+        return new EndpointsResource(this);
     }
 
     public List<EndpointLink> getEndpoints() {
-        return getEndpointsDTO().getEndpoints();
-    }
-
-    /**
-     * Looks up an individual endpoint
-     */
-    @Path("endpoint/{id}")
-    public EndpointResource getEndpoint(@PathParam("id") String id) {
-        // TODO lets assume the ID is the endpoint
-        Endpoint endpoint = getCamelContext().getEndpoint(id);
-        if (endpoint != null) {
-            return new EndpointResource(camelContext, template, endpoint);
-        } else {
-            return null;
-        }
+        return getEndpointsResource().getEndpointsDTO().getEndpoints();
     }
 
     /**
@@ -140,5 +127,4 @@
         return getRouteDefinitions().getRoutes();
     }
 
-
 }

Added: camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/resources/EndpointsResource.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/resources/EndpointsResource.java?rev=745547&view=auto
==============================================================================
--- camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/resources/EndpointsResource.java (added)
+++ camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/resources/EndpointsResource.java Wed Feb 18 15:57:40 2009
@@ -0,0 +1,140 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.rest.resources;
+
+import com.sun.jersey.api.representation.Form;
+import com.sun.jersey.api.view.ImplicitProduces;
+import com.sun.jersey.api.view.Viewable;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.rest.model.EndpointLink;
+import org.apache.camel.rest.model.Endpoints;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+@ImplicitProduces(Constants.HTML_MIME_TYPES)
+public class EndpointsResource {
+    private final CamelContext camelContext;
+    private final ProducerTemplate template;
+    private String error = "";
+    private String newUri = "mock:someName";
+
+    public EndpointsResource(CamelContextResource contextResource) {
+        this.camelContext = contextResource.getCamelContext();
+        this.template = contextResource.getTemplate();
+    }
+
+    /**
+     * Returns a list of endpoints available in this context
+     *
+     * @return
+     */
+    @GET
+    @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
+    public Endpoints getEndpointsDTO() {
+        return new Endpoints(camelContext);
+    }
+
+
+    /**
+     * Looks up an individual endpoint
+     */
+    @Path("{id}")
+    public EndpointResource getEndpoint(@PathParam("id") String id) {
+        // TODO lets assume the ID is the endpoint
+        Endpoint endpoint = null;
+        if (id != null) {
+            // lets remove any whitespace
+            id = id.trim();
+            if (id.length() > 0) {
+                endpoint = camelContext.getEndpoint(id);
+            }
+        }
+        if (endpoint != null) {
+            return new EndpointResource(camelContext, template, endpoint);
+        } else {
+            return null;
+        }
+    }
+
+
+    // Creating endpoints
+    //-------------------------------------------------------------------------
+
+    @POST
+    @Consumes(MediaType.TEXT_PLAIN)
+    public Response postMessage(String uri) throws URISyntaxException {
+        EndpointResource endpoint = getEndpoint(uri);
+        if (endpoint != null) {
+            return Response.ok().build();
+        }
+        return Response.noContent().build();
+    }
+
+    @POST
+    @Consumes("application/x-www-form-urlencoded")
+    public Response processForm(@Context UriInfo uriInfo, Form formData) throws URISyntaxException {
+        System.out.println("Received form! " + formData);
+        newUri = formData.getFirst("uri", String.class);
+        EndpointResource endpoint = getEndpoint(newUri);
+        if (endpoint != null) {
+            String href = endpoint.getHref();
+            System.out.println("Created endpoint so redirecting to " + href);
+            return Response.seeOther(new URI(href)).build();
+        } else {
+            error = "Could not find a component to resolve that URI";
+
+            System.out.println("Failed to create new endpoint!");
+
+            // lets re-render the form
+            return Response.ok(new Viewable("index", this)).build();
+        }
+    }
+
+
+    // Properties
+    //-------------------------------------------------------------------------
+
+    public List<EndpointLink> getEndpoints() {
+        return getEndpointsDTO().getEndpoints();
+    }
+
+    public String getError() {
+        return error;
+    }
+
+    public String getNewUri() {
+        return newUri;
+    }
+}

Propchange: camel/trunk/components/camel-web/src/main/java/org/apache/camel/rest/resources/EndpointsResource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: camel/trunk/components/camel-web/src/main/webapp/WEB-INF/decorators/main.jsp
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/main/webapp/WEB-INF/decorators/main.jsp?rev=745547&r1=745546&r2=745547&view=diff
==============================================================================
--- camel/trunk/components/camel-web/src/main/webapp/WEB-INF/decorators/main.jsp (original)
+++ camel/trunk/components/camel-web/src/main/webapp/WEB-INF/decorators/main.jsp Wed Feb 18 15:57:40 2009
@@ -89,16 +89,13 @@
                   <div class="navigation_top">
                     <div class="navigation_bottom">
 
-<%--
-                      <H3>Queue Views</H3>
+                      <H3>Actions</H3>
 
                       <ul class="alternate" type="square">
+                        <li><a href="<c:url value='/endpoints'/>" title="View current endpoints or create new ones">Endpoints</a></li>
+                      </ul>
 
 
-                        <li><a href="queueGraph.jsp" title="View the queue depths as a graph">Graph</a></li>
-                        <li><a href="xml/queues.jsp" title="View the queues as XML">XML</a></li>
-                      </ul>
---%>
                       <H3>Useful Links</H3>
 
                       <ul class="alternate" type="square">

Modified: camel/trunk/components/camel-web/src/main/webapp/css/type-settings.css
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/main/webapp/css/type-settings.css?rev=745547&r1=745546&r2=745547&view=diff
==============================================================================
--- camel/trunk/components/camel-web/src/main/webapp/css/type-settings.css (original)
+++ camel/trunk/components/camel-web/src/main/webapp/css/type-settings.css Wed Feb 18 15:57:40 2009
@@ -174,4 +174,8 @@
     width: 500px;
     height: 100px;
     overflow : auto;
+}
+
+.error {
+  color: crimson;
 }
\ No newline at end of file

Copied: camel/trunk/components/camel-web/src/main/webapp/org/apache/camel/rest/resources/EndpointsResource/index.jsp (from r745452, camel/trunk/components/camel-web/src/main/webapp/org/apache/camel/rest/resources/CamelContextResource/endpoints.jsp)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/main/webapp/org/apache/camel/rest/resources/EndpointsResource/index.jsp?p2=camel/trunk/components/camel-web/src/main/webapp/org/apache/camel/rest/resources/EndpointsResource/index.jsp&p1=camel/trunk/components/camel-web/src/main/webapp/org/apache/camel/rest/resources/CamelContextResource/endpoints.jsp&r1=745452&r2=745547&rev=745547&view=diff
==============================================================================
--- camel/trunk/components/camel-web/src/main/webapp/org/apache/camel/rest/resources/CamelContextResource/endpoints.jsp (original)
+++ camel/trunk/components/camel-web/src/main/webapp/org/apache/camel/rest/resources/EndpointsResource/index.jsp Wed Feb 18 15:57:40 2009
@@ -13,6 +13,33 @@
   </c:forEach>
 </ul>
 
+<h2>Create New Endpoint</h2>
+
+<form action="/endpoints" method="post">
+  <table>
+    <c:if test="${not empty it.error}">
+      <tr>
+        <td colspan="2" align="center" class="error">
+           ${it.error}
+        </td>
+      </tr>
+    </c:if>
+    <tr>
+      <td>
+        Please enter the new endpoint URI
+      </td>
+      <td>
+        <input type="text" name="uri" value="${it.newUri}" width="80">
+      </td>
+    </tr>
+    <tr>
+      <td colspan="2" align="center">
+         <input type="submit" value="Create"> <input type="reset">
+      </td>
+    </tr>
+  </table>
+</form>
+        
 
 </body>
 </html>

Propchange: camel/trunk/components/camel-web/src/main/webapp/org/apache/camel/rest/resources/EndpointsResource/index.jsp
------------------------------------------------------------------------------
    svn:eol-style = native