-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7765220
Showing
9 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>me.ravikanth</groupId> | ||
<artifactId>qodtest</artifactId> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>qodtest Maven Webapp</name> | ||
<url>http://maven.apache.org</url> | ||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.glassfish.jersey.containers</groupId> | ||
<artifactId>jersey-container-servlet</artifactId> | ||
<version>2.22</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.containers</groupId> | ||
<artifactId>jersey-container-servlet-core</artifactId> | ||
<version>2.22</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.glassfish.jersey.media</groupId> | ||
<artifactId>jersey-media-json-jackson</artifactId> | ||
<version>2.22</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mongodb</groupId> | ||
<artifactId>mongo-java-driver</artifactId> | ||
<version>3.0.4</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
<build> | ||
<finalName>qodtest</finalName> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package me.ravikanth.qodtest.dao; | ||
|
||
import me.ravikanth.qodtest.resource.Quote; | ||
|
||
/** | ||
* Created by ragudipati on 11/30/15. | ||
*/ | ||
public interface QuoteDAO { | ||
public Quote insert(Quote quote); | ||
public Quote getQuote(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package me.ravikanth.qodtest.dao; | ||
|
||
import com.mongodb.BasicDBObject; | ||
import com.mongodb.DB; | ||
import com.mongodb.DBCollection; | ||
import com.mongodb.MongoClient; | ||
import me.ravikanth.qodtest.resource.Quote; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by ragudipati on 11/30/15. | ||
*/ | ||
public class QuoteDAOImpl implements QuoteDAO { | ||
public Quote insert(Quote quote) { | ||
|
||
Quote ret = null; | ||
MongoClient mongoClient = null; | ||
try{ | ||
// Connect to MongoDB | ||
mongoClient = new MongoClient("localhost", 27017); | ||
DB db = (DB) mongoClient.getDB("qod"); | ||
DBCollection dbCollection = db.getCollection("qod_unused"); | ||
|
||
BasicDBObject doc = new BasicDBObject(); | ||
for(String key : quote.getFields().keySet()){ | ||
doc.append(key, quote.getFields().get(key)); | ||
} | ||
dbCollection.insert(doc); | ||
mongoClient.close(); | ||
ret = quote; | ||
}catch (Exception e){ | ||
System.err.println(e.getClass().getName() + " : " + e.getMessage()); | ||
}finally { | ||
if(mongoClient != null){ | ||
mongoClient.close(); | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
public Quote getQuote() { | ||
Quote ret = null; | ||
MongoClient mongoClient = null; | ||
|
||
try{ | ||
mongoClient = new MongoClient("localhost", 27017); | ||
DB db = (DB) mongoClient.getDB("qod"); | ||
DBCollection unusedDBCollection = (DBCollection) db.getCollection("qod_unused"); | ||
DBCollection usedDBCollection = (DBCollection) db.getCollection("qod_used"); | ||
|
||
BasicDBObject doc = (BasicDBObject) unusedDBCollection.findOne(); | ||
|
||
ret = new Quote(); | ||
Map<String, String> fields = new HashMap<String , String>(); | ||
|
||
for(Map.Entry<String, Object> entry : doc.entrySet()){ | ||
if(!entry.getKey().contains("_id")) | ||
fields.put(entry.getKey(), (String) entry.getValue()); | ||
} | ||
ret.setFields(fields); | ||
|
||
unusedDBCollection.remove(doc); | ||
usedDBCollection.insert(doc); | ||
mongoClient.close(); | ||
|
||
}catch(Exception e){ | ||
System.err.println(e.getClass().getName() + " : " + e.getMessage()); | ||
}finally { | ||
if(mongoClient != null){ | ||
mongoClient.close(); | ||
} | ||
} | ||
return ret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package me.ravikanth.qodtest.resource; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Created by ragudipati on 11/30/15. | ||
*/ | ||
public class Quote { | ||
private Map<String, String> fields; | ||
|
||
|
||
public Map<String, String> getFields() { | ||
return fields; | ||
} | ||
|
||
public void setFields(Map<String, String> fields) { | ||
this.fields = fields; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/me/ravikanth/qodtest/resource/QuoteManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package me.ravikanth.qodtest.resource; | ||
|
||
import me.ravikanth.qodtest.resource.Quote; | ||
|
||
/** | ||
* Created by ragudipati on 11/28/15. | ||
*/ | ||
public interface QuoteManager { | ||
public Quote postQuote(Quote quote); | ||
public Quote getQuote(); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/me/ravikanth/qodtest/resource/QuoteManagerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package me.ravikanth.qodtest.resource; | ||
|
||
import me.ravikanth.qodtest.dao.QuoteDAO; | ||
import me.ravikanth.qodtest.dao.QuoteDAOImpl; | ||
|
||
/** | ||
* Created by ragudipati on 11/30/15. | ||
*/ | ||
public class QuoteManagerImpl implements QuoteManager { | ||
public Quote postQuote(Quote quote) { | ||
Quote ret = null; | ||
QuoteDAO quoteDAO = new QuoteDAOImpl(); | ||
ret = quoteDAO.insert(quote); | ||
return ret; | ||
} | ||
|
||
public Quote getQuote() { | ||
Quote ret = null; | ||
QuoteDAO quoteDAO = new QuoteDAOImpl(); | ||
ret = quoteDAO.getQuote(); | ||
return ret; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/me/ravikanth/qodtest/service/QodService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package me.ravikanth.qodtest.service; | ||
|
||
import me.ravikanth.qodtest.resource.Quote; | ||
import me.ravikanth.qodtest.resource.QuoteManager; | ||
import me.ravikanth.qodtest.resource.QuoteManagerImpl; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.Path; | ||
|
||
/** | ||
* Created by ragudipati on 11/30/15. | ||
*/ | ||
// The Java class will be hosted at the URI path "/helloworld" | ||
@Path("/quote") | ||
public class QodService { | ||
// The Java method will process HTTP GET requests | ||
@GET | ||
@Path("/getTest") | ||
// The Java method will produce content identified by the MIME Media type "text/plain" | ||
@Produces("application/json") | ||
public String getClichedMessage() { | ||
// Return some cliched textual content | ||
return "Hello World from qodtest"; | ||
} | ||
|
||
|
||
@GET | ||
@Produces("application/json") | ||
public Quote getQuote(){ | ||
Quote ret = null; | ||
QuoteManager quoteManager = new QuoteManagerImpl(); | ||
ret = quoteManager.getQuote(); | ||
return ret; | ||
} | ||
|
||
@POST | ||
@Produces("application/json") | ||
public Quote postTest(Quote quote){ | ||
Quote ret = null; | ||
for(String key : quote.getFields().keySet()){ | ||
System.out.println(key + ":"+quote.getFields().get(key)); | ||
} | ||
QuoteManager quoteManager = new QuoteManagerImpl(); | ||
ret = quoteManager.postQuote(quote); | ||
return ret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<!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> | ||
<display-name>Archetype Created Web Application</display-name> | ||
|
||
<servlet> | ||
<servlet-name>qodtest-servlet</servlet-name> | ||
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> | ||
|
||
<init-param> | ||
<param-name>jersey.config.server.provider.packages</param-name> | ||
<param-value>me.ravikanth.qodtest</param-value> | ||
</init-param> | ||
|
||
<load-on-startup>1</load-on-startup> | ||
</servlet> | ||
|
||
|
||
<servlet-mapping> | ||
<servlet-name>qodtest-servlet</servlet-name> | ||
<url-pattern>/api/*</url-pattern> | ||
</servlet-mapping> | ||
</web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
<h2>Hello World!</h2> | ||
</body> | ||
</html> |