Skip to content

Commit

Permalink
Standalone and deployable RESTful services
Browse files Browse the repository at this point in the history
  • Loading branch information
Maria Voinea authored and Maria Voinea committed Mar 16, 2014
1 parent c767edb commit 8d9e5a9
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 71 deletions.
5 changes: 5 additions & 0 deletions crowdchef-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>1.7.1</version>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.crowdchef.core;


import com.crowdchef.core.handlers.RecipeHandler;
import com.crowdchef.core.handlers.UserHandler;
import com.crowdchef.core.retriever.Indexer;
import com.crowdchef.core.retriever.Searcher;
import com.crowdchef.datamodel.CrowdChefDatabase;
import com.crowdchef.datamodel.entities.Recipe;
import com.crowdchef.datamodel.entities.User;
import com.crowdchef.datamodel.entities.UserInfo;
import com.google.gson.*;
import org.json.JSONObject;

import java.util.List;

public class CoreController {
private CrowdChefDatabase database;
private UserHandler userHandler;
private RecipeHandler recipeHandler;
private final Searcher searcher;
private final Indexer indexer;

public CoreController() {
this.database = new CrowdChefDatabase();
this.userHandler = new UserHandler(this.database);
this.recipeHandler = new RecipeHandler(this.database);
this.searcher = new Searcher();
this.indexer = new Indexer();
}

public static Gson buildGson(final Class<?> skippedFieldClass){
return new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {

public boolean shouldSkipClass(Class<?> clazz) {
return false;
}

public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaredClass() == skippedFieldClass;
}

})
.create();
}

public JsonElement listRecipes(){
Gson gson = new Gson();
List<Recipe> recipes = recipeHandler.getRecipes();
return buildGson(User.class).toJsonTree(recipes);
}

public JsonElement getUserInfo(Long id) {
return buildGson(User.class).toJsonTree(userHandler.getUserInfo(id));
}

public JsonElement search(String searchQuery, String field) {
searcher.search(searchQuery, field);
return null;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.crowdchef.core.handlers;


import com.crowdchef.core.DatabaseController;
import com.crowdchef.core.CoreController;
import com.crowdchef.datamodel.CrowdChefDatabase;
import com.crowdchef.datamodel.daos.RecipeDAO;
import com.crowdchef.datamodel.entities.Recipe;

Expand All @@ -10,8 +11,8 @@
public class RecipeHandler {
private RecipeDAO recipeDao;

public RecipeHandler(DatabaseController databaseController) {
this.recipeDao = new RecipeDAO(databaseController.getDatabase());
public RecipeHandler(CrowdChefDatabase database) {
this.recipeDao = new RecipeDAO(database);

}

Expand All @@ -27,7 +28,11 @@ public Recipe getRecipe(Long id) {
return recipeDao.getRecipe(id);
}

public List<Recipe> getRecipesMatching(String name) {
return recipeDao.getRecipesMatching(name);
public List<Recipe> getRecipes() {
return recipeDao.getRecipesByName("%");
}

public List<Recipe> getRecipesByName(String name) {
return recipeDao.getRecipesByName(name);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.crowdchef.core.handlers;

import com.crowdchef.core.DatabaseController;
import com.crowdchef.core.CoreController;
import com.crowdchef.datamodel.CrowdChefDatabase;
import com.crowdchef.datamodel.ValidationException;
import com.crowdchef.datamodel.daos.UserDAO;
import com.crowdchef.datamodel.entities.User;
Expand All @@ -9,8 +10,8 @@
public class UserHandler {
private UserDAO userDao;

public UserHandler(DatabaseController databaseController) throws ValidationException {
this.userDao = new UserDAO(databaseController.getDatabase());
public UserHandler(CrowdChefDatabase database) throws ValidationException {
this.userDao = new UserDAO(database);
}

public Long registerUser(String username, String password) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.crowdchef.core.test;


import com.crowdchef.core.CoreController;

public class CoreControllerTester {
public static void main(String[] args) {
CoreController controller = new CoreController();
System.out.println(controller.getUserInfo(new Long(1)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ else if (result.size() > 1)
return result.get(0);
}

public List<Recipe> getRecipesMatching(String name) {
public List<Recipe> getRecipesByName(String name) {
List<Recipe> result = database.retrieve("AllRecipesLikeName", "name", name, Recipe.class);
return result;
}
Expand Down
31 changes: 29 additions & 2 deletions crowdchef-webservice/crowdchef-webservice.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.sparkjava:spark-core:1.1" level="project" />
<orderEntry type="library" name="Maven: com.sparkjava:spark-core:1.0" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.2" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-server:9.0.2.v20130417" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jetty.orbit:javax.servlet:3.0.0.v201112011016" level="project" />
Expand All @@ -24,6 +24,33 @@
<orderEntry type="library" name="Maven: org.eclipse.jetty:jetty-security:9.0.2.v20130417" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-core-asl:1.9.13" level="project" />
<orderEntry type="library" name="Maven: org.codehaus.jackson:jackson-mapper-asl:1.9.13" level="project" />
<orderEntry type="library" name="Maven: crowdchef-core:crowdchef-core:1.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: crowdchef-datamodel:crowdchef-datamodel:1.0-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.26" level="project" />
<orderEntry type="library" name="Maven: org.hibernate:hibernate-core:4.2.5.Final" level="project" />
<orderEntry type="library" name="Maven: antlr:antlr:2.7.7" level="project" />
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.1.0.GA" level="project" />
<orderEntry type="library" name="Maven: dom4j:dom4j:1.6.1" level="project" />
<orderEntry type="library" name="Maven: org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:1.0.1.Final" level="project" />
<orderEntry type="library" name="Maven: org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final" level="project" />
<orderEntry type="library" name="Maven: org.hibernate.common:hibernate-commons-annotations:4.0.2.Final" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.15.0-GA" level="project" />
<orderEntry type="library" name="Maven: org.hibernate:hibernate-entitymanager:4.2.5.Final" level="project" />
<orderEntry type="library" name="Maven: org.hibernate:hibernate-validator:5.0.1.Final" level="project" />
<orderEntry type="library" name="Maven: javax.validation:validation-api:1.1.0.Final" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml:classmate:0.8.0" level="project" />
<orderEntry type="library" name="Maven: cglib:cglib:2.2" level="project" />
<orderEntry type="library" name="Maven: asm:asm:3.1" level="project" />
<orderEntry type="library" name="Maven: javax.transaction:jta:1.1" level="project" />
<orderEntry type="library" name="Maven: com.intellij:annotations:12.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.lucene:lucene-core:4.4.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.lucene:lucene-analyzers-common:4.4.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.lucene:lucene-queryparser:4.4.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.lucene:lucene-queries:4.4.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.lucene:lucene-sandbox:4.4.0" level="project" />
<orderEntry type="library" name="Maven: jakarta-regexp:jakarta-regexp:1.4" level="project" />
<orderEntry type="library" name="Maven: org.json:json:20090211" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:1.7.1" level="project" />
</component>
</module>

24 changes: 22 additions & 2 deletions crowdchef-webservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>


<groupId>crowdchef-webservice</groupId>
<artifactId>crowdchef-webservice</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>1.1</version>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
Expand All @@ -23,7 +25,25 @@
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>crowdchef-core</groupId>
<artifactId>crowdchef-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>1.7.1</version>
</dependency>
</dependencies>

<build>
<finalName>CrowdChef</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.crowdchef.webservice;

import com.crowdchef.*;

import static spark.Spark.setPort;

public class CrowdChefRESTServerMain {
public static void main(String[] args)
{
public static void main(String[] args) {
setPort(9090);
new WebService(new Searcher(), new Indexer()).start();
new WebService().init();

}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
package com.crowdchef.webservice;

import com.crowdchef.*;
import spark.*;
import com.crowdchef.core.CoreController;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.servlet.SparkApplication;

import static spark.Spark.get;

public class WebService {
private final Searcher theSearcher;
private final Indexer theIndexer;
public class WebService implements SparkApplication {
private CoreController controller;

public WebService(final Searcher aSearcher, final Indexer aIndexer)
{
theSearcher = aSearcher;
theIndexer = aIndexer;
}
@Override
public void init() {

controller = new CoreController();

public void start()
{
get(new Route("/")
{
get(new Route("/") {
@Override
public Object handle(final Request request, final Response response)
{
public Object handle(final Request request, final Response response) {
return "CrowdChefServer is On!";
}
});

get(new Route("/search/:searchQuery/:field")
{
get(new Route("/listRecipes") {
@Override
public Object handle(final Request request, final Response response) {
return controller.listRecipes();
}
});

get(new Route("/getUserInfo/:id") {
@Override
public Object handle(final Request request, final Response response) {
return controller.getUserInfo(Long.parseLong(request.params("id")));
}
});

get(new Route("/search/:searchQuery/:field") {
@Override
public Object handle(final Request request, final Response response)
{
public Object handle(final Request request, final Response response) {

theSearcher.search(request.params("searchQuery"),
request.params("field"));
controller.search(request.params("searchQuery"),
request.params("field"));
return "CrowdChefServer is On!";
}
});
Expand Down
19 changes: 19 additions & 0 deletions crowdchef-webservice/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<filter>
<filter-name>SparkFilter</filter-name>
<filter-class>spark.servlet.SparkFilter</filter-class>
<init-param>
<param-name>applicationClass</param-name>
<param-value>com.crowdchef.webservice.WebService</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>SparkFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

0 comments on commit 8d9e5a9

Please sign in to comment.