You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by tb...@apache.org on 2014/05/20 16:18:07 UTC

git commit: AMBARI-5795 - Add Favorite views example (Jeff Sposetti via tbeerbower)

Repository: ambari
Updated Branches:
  refs/heads/trunk 67c507014 -> ad3ff7ed6


AMBARI-5795 - Add Favorite views example (Jeff Sposetti via tbeerbower)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/ad3ff7ed
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/ad3ff7ed
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/ad3ff7ed

Branch: refs/heads/trunk
Commit: ad3ff7ed62a6ffb87af041593dcb30918b24db56
Parents: 67c5070
Author: tbeerbower <tb...@hortonworks.com>
Authored: Tue May 20 10:17:14 2014 -0400
Committer: tbeerbower <tb...@hortonworks.com>
Committed: Tue May 20 10:17:49 2014 -0400

----------------------------------------------------------------------
 ambari-views/examples/README.md                 |   2 +
 .../examples/favorite-view/docs/index.md        |  27 ++++
 ambari-views/examples/favorite-view/pom.xml     |  89 +++++++++++++
 .../ambari/view/favorite/FavoriteService.java   | 131 +++++++++++++++++++
 .../favorite-view/src/main/resources/view.xml   |  38 ++++++
 ambari-views/examples/pom.xml                   |   1 +
 6 files changed, 288 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/ad3ff7ed/ambari-views/examples/README.md
----------------------------------------------------------------------
diff --git a/ambari-views/examples/README.md b/ambari-views/examples/README.md
index 8773c4d..4b65216 100644
--- a/ambari-views/examples/README.md
+++ b/ambari-views/examples/README.md
@@ -23,7 +23,9 @@ See the documentation pages for the view examples.
 
 * [Hello World View](helloworld-view/docs/index.md) : Demonstrates the very basics of how to write and deploy a view in Ambari.
 * [Hello Servlet View](hello-servlet-view/docs/index.md) : Includes instance parameters and a servlet for a dynamic UI. 
+* [Favorite view](favorite-view/docs/index.md) : Exposes a simple resource to work with instance parameters and data
 * [Calculator View](calculator-view/docs/index.md) : Includes a simple resource.
+* [Phone List View](phone-list-view/docs/index.md) : Demonstrates simple view persistence.
 * [Weather view](weather-view/docs/index.md)
 
 Please also visit the [Apache Ambari Project](http://ambari.apache.org/) page for more information.

http://git-wip-us.apache.org/repos/asf/ambari/blob/ad3ff7ed/ambari-views/examples/favorite-view/docs/index.md
----------------------------------------------------------------------
diff --git a/ambari-views/examples/favorite-view/docs/index.md b/ambari-views/examples/favorite-view/docs/index.md
new file mode 100644
index 0000000..a3990d7
--- /dev/null
+++ b/ambari-views/examples/favorite-view/docs/index.md
@@ -0,0 +1,27 @@
+<!---
+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](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.
+-->
+
+Favorite View Example
+======
+
+Description
+-----
+The Favoriate View is a very simple view example. It demonstrates the very basics of how
+to write and expose a REST service which works with instance parameters and instance data.
+
+Package
+-----
+All views are packaged as a view archive. The view archive contains the configuration
+file and various optional components of the view.

http://git-wip-us.apache.org/repos/asf/ambari/blob/ad3ff7ed/ambari-views/examples/favorite-view/pom.xml
----------------------------------------------------------------------
diff --git a/ambari-views/examples/favorite-view/pom.xml b/ambari-views/examples/favorite-view/pom.xml
new file mode 100644
index 0000000..2d64dd9
--- /dev/null
+++ b/ambari-views/examples/favorite-view/pom.xml
@@ -0,0 +1,89 @@
+<!--
+   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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <parent>
+    <groupId>org.apache.ambari</groupId>
+    <artifactId>ambari-view-examples</artifactId>
+    <version>1.0.0</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <artifactId>favorite-view</artifactId>
+  <packaging>jar</packaging>
+  <name>Ambari Favorite View</name>
+  <url>http://maven.apache.org</url>
+  <properties>
+    <ambari.version>1.3.0-SNAPSHOT</ambari.version>
+  </properties>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.easymock</groupId>
+      <artifactId>easymock</artifactId>
+      <version>3.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.ambari</groupId>
+      <artifactId>ambari-views</artifactId>
+      <version>${ambari.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-server</artifactId>
+      <version>1.8</version>
+    </dependency>
+    <dependency>
+      <groupId>javax.inject</groupId>
+      <artifactId>javax.inject</artifactId>
+      <version>1</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-surefire-plugin</artifactId>
+          <version>2.12</version>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+    <plugins>
+        <plugin>
+        <groupId>org.codehaus.mojo</groupId>
+        <artifactId>rpm-maven-plugin</artifactId>
+        <version>2.0.1</version>
+        <executions>
+          <execution>
+            <phase>none</phase>
+            <goals>
+              <goal>rpm</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>

http://git-wip-us.apache.org/repos/asf/ambari/blob/ad3ff7ed/ambari-views/examples/favorite-view/src/main/java/org/apache/ambari/view/favorite/FavoriteService.java
----------------------------------------------------------------------
diff --git a/ambari-views/examples/favorite-view/src/main/java/org/apache/ambari/view/favorite/FavoriteService.java b/ambari-views/examples/favorite-view/src/main/java/org/apache/ambari/view/favorite/FavoriteService.java
new file mode 100644
index 0000000..171b23a
--- /dev/null
+++ b/ambari-views/examples/favorite-view/src/main/java/org/apache/ambari/view/favorite/FavoriteService.java
@@ -0,0 +1,131 @@
+/**
+ * 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.ambari.view.favorite;
+
+import org.apache.ambari.view.ViewContext;
+
+import javax.inject.Inject;
+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.Consumes;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+import java.io.IOException;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * The Favorite service.
+ */
+public class FavoriteService {
+
+  private   static  final   String  PROPERTY_QUESTION = "what.is.the.question";
+  private   static  final   String  PROPERTY_DONOTKNOW = "i.do.not.know";
+
+  private   static  final   String  INSTANCE_ITEM = "favorite.item";
+  
+  /**
+   * The view context.
+   */
+  @Inject
+  ViewContext context;
+
+  /**
+   * Handles: POST /item/{thing}.
+   *
+   * @param headers   http headers
+   * @param ui        uri info
+   *
+   * @return favorite item representation
+   */
+  @POST
+  @Path("/item/{thing}")
+  @Consumes({"text/plain", "application/json"})
+  @Produces({"text/plain", "application/json"})
+  public Response setFavoriteItem(@Context HttpHeaders headers, @Context UriInfo ui,
+                            @PathParam("thing") String thing) {
+                            
+    context.putInstanceData(INSTANCE_ITEM, thing);
+    
+    StringBuffer buf = new StringBuffer();
+    buf.append("{\"item\" : \"");
+    buf.append(thing);
+    buf.append("\"}");
+
+    return Response.ok(buf.toString()).build();
+  }
+  
+  /**
+   * Handles: GET /item.
+   *
+   * @param headers   http headers
+   * @param ui        uri info
+   *
+   * @return favorite item representation
+   */
+  @GET
+  @Path("/item")
+  @Produces({"text/plain", "application/json"})
+  public Response getFavoriteItem(@Context HttpHeaders headers, @Context UriInfo ui) {
+    System.out.println(" context = "+context);
+
+    String item = context.getInstanceData(INSTANCE_ITEM);
+    if (item == null || item.isEmpty())
+        item = context.getProperties().get(PROPERTY_DONOTKNOW);
+
+    StringBuffer buf = new StringBuffer();
+    buf.append("{\"item\" : \"");
+    buf.append(item);
+    buf.append("\"}");
+
+    return Response.ok(buf.toString()).build();
+  }
+
+  /**
+   * Handles: GET /question.
+   *
+   * @param headers   http headers
+   * @param ui        uri info
+   *
+   * @return favorite question representation
+   */
+  @GET
+  @Path("/question")
+  @Produces({"text/plain", "application/json"})
+  public Response getQuestion(@Context HttpHeaders headers, @Context UriInfo ui) {
+
+    String  question = context.getProperties().get(PROPERTY_QUESTION);
+    String  username = context.getUsername();
+
+    StringBuffer buf = new StringBuffer();
+    buf.append("{\"question\" : \"");
+    buf.append(question);
+    buf.append("\", \"username\" : \"");
+    buf.append(username);
+    buf.append("\"}");
+        
+    return Response.ok(buf.toString()).build();
+  }
+
+
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/ad3ff7ed/ambari-views/examples/favorite-view/src/main/resources/view.xml
----------------------------------------------------------------------
diff --git a/ambari-views/examples/favorite-view/src/main/resources/view.xml b/ambari-views/examples/favorite-view/src/main/resources/view.xml
new file mode 100644
index 0000000..f289536
--- /dev/null
+++ b/ambari-views/examples/favorite-view/src/main/resources/view.xml
@@ -0,0 +1,38 @@
+<!--
+   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.
+-->
+<view>
+  <name>FAVORITE</name>
+  <label>Favorite</label>
+  <version>0.1.0</version>
+
+  <parameter>
+    <name>what.is.the.question</name>
+    <description>Ask a question</description>
+    <required>true</required>
+  </parameter>
+  <parameter>
+    <name>i.do.not.know</name>
+    <description>If you do not know</description>
+    <required>true</required>
+  </parameter>
+
+  <resource>
+    <name>favorite</name>
+    <service-class>org.apache.ambari.view.examples.FavoriteService</service-class>
+  </resource>
+
+</view>

http://git-wip-us.apache.org/repos/asf/ambari/blob/ad3ff7ed/ambari-views/examples/pom.xml
----------------------------------------------------------------------
diff --git a/ambari-views/examples/pom.xml b/ambari-views/examples/pom.xml
index 7309ef5..0951f19 100644
--- a/ambari-views/examples/pom.xml
+++ b/ambari-views/examples/pom.xml
@@ -25,6 +25,7 @@
   <modules>
     <module>helloworld-view</module>
     <module>hello-servlet-view</module>
+    <module>favorite-view</module>
     <module>phone-list-view</module>
     <module>calculator-view</module>
     <module>weather-view</module>