Created
February 18, 2014 12:28
-
-
Save pablomendes/9070062 to your computer and use it in GitHub Desktop.
REST server stub for your OKBQA service
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
package rest; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.ws.rs.*; | |
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 okbqa.verbs.Server; | |
/** | |
* Dummy Web Service | |
*/ | |
@ApplicationPath(Server.APP_PATH) | |
@Path("/hello") | |
@Consumes("text/plain") | |
public class HelloWorld { | |
@Context | |
private UriInfo context; | |
@GET | |
@Produces({MediaType.APPLICATION_JSON}) | |
public Response getXML(@DefaultValue("") @QueryParam("question") String text, | |
@Context HttpServletRequest request) { | |
try { | |
String clientIp = request.getRemoteAddr(); | |
return Response.ok().entity("{'ip': '"+clientIp+"', 'msg': '"+text+"'}").header("Access-Control-Allow-Origin","*").build(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(e).type(MediaType.APPLICATION_JSON).build()); | |
} | |
} | |
@POST | |
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | |
@Produces({MediaType.TEXT_XML,MediaType.APPLICATION_XML}) | |
public Response postXML(@DefaultValue("") @FormParam("question") String text, | |
@Context HttpServletRequest request) { | |
try { | |
String clientIp = request.getRemoteAddr(); | |
return Response.ok().entity("{'ip': '"+clientIp+"', 'msg': '"+text+"'}").header("Access-Control-Allow-Origin","*").build(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(e).type(MediaType.APPLICATION_JSON).build()); | |
} | |
} | |
} |
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
import com.sun.grizzly.http.SelectorThread; | |
import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class Server { | |
final public static String APP_PATH = "http://localhost:2020/rest/"; | |
public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException, ClassNotFoundException { | |
if (args.length>1) { | |
} | |
URI serverURI = new URI(APP_PATH); | |
final Map<String, String> initParams = new HashMap<String, String>(); | |
initParams.put("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig"); | |
initParams.put("com.sun.jersey.config.property.packages", "rest"); | |
SelectorThread threadSelector = GrizzlyWebContainerFactory.create(serverURI, initParams); | |
threadSelector.start(); | |
System.err.println("Server started in " + System.getProperty("user.dir") + " listening on " + serverURI); | |
boolean running = true; | |
while(running) { | |
Thread.sleep(100); | |
} | |
//Stop the HTTP server | |
//server.stop(0); | |
threadSelector.stopEndpoint(); | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment