From 66b0584bd4a378e0937f5e61b3cb223b1bf1fd93 Mon Sep 17 00:00:00 2001 From: Brian Kennedy Date: Thu, 14 Nov 2024 16:42:43 -0800 Subject: [PATCH] #188571765 Additional Jersey Example using code and annotations to set up and configure (#137) * Moving jersey example to an xml specific folder and creating a Jersey 2.3.0 example using annotations * Explicitly annotate a ServletContainer to configure the application path * Cleanup --- .../README.md | 0 jersey-servlet-example-annotations/pom.xml | 132 ++++++++++++++++++ .../jersey/example/CompaniesServlet.java | 0 .../servlet/jersey/example/FileUpload.java | 0 .../servlet/jersey/example/JerseyDemo.java | 0 .../servlet/jersey/example/JerseyServlet.java | 19 +++ .../example/MoesifJerseyApplication.java | 17 +++ .../jersey/example/MoesifServletFilter.java | 55 ++++++++ .../jersey/example/SubscriptionsServlet.java | 0 .../servlet/jersey/example/UsersServlet.java | 0 .../src/main/webapp/file-upload.html | 0 .../src/main/webapp/index.html | 0 jersey-servlet-example-xml/README.md | 68 +++++++++ .../pom.xml | 6 +- .../jersey/example/CompaniesServlet.java | 83 +++++++++++ .../servlet/jersey/example/FileUpload.java | 55 ++++++++ .../servlet/jersey/example/JerseyDemo.java | 54 +++++++ .../jersey/example/SubscriptionsServlet.java | 73 ++++++++++ .../servlet/jersey/example/UsersServlet.java | 84 +++++++++++ .../src/main/webapp/WEB-INF/web.xml | 0 .../src/main/webapp/file-upload.html | 15 ++ .../src/main/webapp/index.html | 8 ++ 22 files changed, 666 insertions(+), 3 deletions(-) rename {jersey-servlet-example => jersey-servlet-example-annotations}/README.md (100%) create mode 100644 jersey-servlet-example-annotations/pom.xml rename {jersey-servlet-example => jersey-servlet-example-annotations}/src/main/java/com/moesif/servlet/jersey/example/CompaniesServlet.java (100%) rename {jersey-servlet-example => jersey-servlet-example-annotations}/src/main/java/com/moesif/servlet/jersey/example/FileUpload.java (100%) rename {jersey-servlet-example => jersey-servlet-example-annotations}/src/main/java/com/moesif/servlet/jersey/example/JerseyDemo.java (100%) create mode 100644 jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/JerseyServlet.java create mode 100644 jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/MoesifJerseyApplication.java create mode 100644 jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/MoesifServletFilter.java rename {jersey-servlet-example => jersey-servlet-example-annotations}/src/main/java/com/moesif/servlet/jersey/example/SubscriptionsServlet.java (100%) rename {jersey-servlet-example => jersey-servlet-example-annotations}/src/main/java/com/moesif/servlet/jersey/example/UsersServlet.java (100%) rename {jersey-servlet-example => jersey-servlet-example-annotations}/src/main/webapp/file-upload.html (100%) rename {jersey-servlet-example => jersey-servlet-example-annotations}/src/main/webapp/index.html (100%) create mode 100644 jersey-servlet-example-xml/README.md rename {jersey-servlet-example => jersey-servlet-example-xml}/pom.xml (96%) create mode 100644 jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/CompaniesServlet.java create mode 100644 jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/FileUpload.java create mode 100644 jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/JerseyDemo.java create mode 100644 jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/SubscriptionsServlet.java create mode 100644 jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/UsersServlet.java rename {jersey-servlet-example => jersey-servlet-example-xml}/src/main/webapp/WEB-INF/web.xml (100%) create mode 100644 jersey-servlet-example-xml/src/main/webapp/file-upload.html create mode 100644 jersey-servlet-example-xml/src/main/webapp/index.html diff --git a/jersey-servlet-example/README.md b/jersey-servlet-example-annotations/README.md similarity index 100% rename from jersey-servlet-example/README.md rename to jersey-servlet-example-annotations/README.md diff --git a/jersey-servlet-example-annotations/pom.xml b/jersey-servlet-example-annotations/pom.xml new file mode 100644 index 0000000..2c96779 --- /dev/null +++ b/jersey-servlet-example-annotations/pom.xml @@ -0,0 +1,132 @@ + + + 4.0.0 + + com.moesif.servlet.jersey + jersey-servlet-example-annotations + war + 1.0.0 + jersey-servlet-example-annotations + + + 2.30 + UTF-8 + 11 + 11 + + + + jersey-servlet-example-annotations + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + true + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + org.apache.maven.plugins + maven-war-plugin + 3.3.2 + + + + org.apache.maven.plugins + maven-dependency-plugin + 3.3.0 + + + package + + copy + + + + + com.heroku + webapp-runner-main + 8.5.68.1 + webapp-runner.jar + + + + + + + + + + + + + + org.glassfish.jersey + jersey-bom + ${jersey.version} + pom + import + + + + + + + org.glassfish.jersey.containers + jersey-container-servlet-core + + + + + org.glassfish.jersey.media + jersey-media-multipart + ${jersey.version} + + + + org.glassfish.jersey.inject + jersey-hk2 + ${jersey.version} + + + + + com.moesif.servlet + moesif-servlet + 1.8.1 + + + + com.sun.xml.bind + jaxb-core + 2.3.0.1 + + + javax.xml.bind + jaxb-api + 2.3.1 + + + com.sun.xml.bind + jaxb-impl + 2.3.6 + + + javax.servlet + javax.servlet-api + 3.1.0 + compile + + + + diff --git a/jersey-servlet-example/src/main/java/com/moesif/servlet/jersey/example/CompaniesServlet.java b/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/CompaniesServlet.java similarity index 100% rename from jersey-servlet-example/src/main/java/com/moesif/servlet/jersey/example/CompaniesServlet.java rename to jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/CompaniesServlet.java diff --git a/jersey-servlet-example/src/main/java/com/moesif/servlet/jersey/example/FileUpload.java b/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/FileUpload.java similarity index 100% rename from jersey-servlet-example/src/main/java/com/moesif/servlet/jersey/example/FileUpload.java rename to jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/FileUpload.java diff --git a/jersey-servlet-example/src/main/java/com/moesif/servlet/jersey/example/JerseyDemo.java b/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/JerseyDemo.java similarity index 100% rename from jersey-servlet-example/src/main/java/com/moesif/servlet/jersey/example/JerseyDemo.java rename to jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/JerseyDemo.java diff --git a/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/JerseyServlet.java b/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/JerseyServlet.java new file mode 100644 index 0000000..91f9302 --- /dev/null +++ b/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/JerseyServlet.java @@ -0,0 +1,19 @@ +package com.moesif.servlet.jersey.example; + +import javax.servlet.annotation.WebServlet; +import javax.servlet.annotation.WebInitParam; +import org.glassfish.jersey.servlet.ServletContainer; + +@WebServlet( + urlPatterns = {"/api/*"}, + initParams = { + @WebInitParam( + name = "javax.ws.rs.Application", + value = "com.moesif.servlet.jersey.example.MoesifJerseyApplication" + ) + }, + loadOnStartup = 1 +) +public class JerseyServlet extends ServletContainer { + // No additional code needed here +} diff --git a/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/MoesifJerseyApplication.java b/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/MoesifJerseyApplication.java new file mode 100644 index 0000000..a04f1ba --- /dev/null +++ b/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/MoesifJerseyApplication.java @@ -0,0 +1,17 @@ +package com.moesif.servlet.jersey.example; + +import org.glassfish.jersey.media.multipart.MultiPartFeature; +import org.glassfish.jersey.server.ResourceConfig; + +import java.util.logging.Logger; + + +public class MoesifJerseyApplication extends ResourceConfig { + Logger log = Logger.getLogger(MoesifJerseyApplication.class.getName()); + + public MoesifJerseyApplication() { + log.info("Registering MoesifJerseyApplication"); + packages("com.moesif.servlet.jersey.example"); + register(MultiPartFeature.class); + } +} \ No newline at end of file diff --git a/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/MoesifServletFilter.java b/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/MoesifServletFilter.java new file mode 100644 index 0000000..559b887 --- /dev/null +++ b/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/MoesifServletFilter.java @@ -0,0 +1,55 @@ +package com.moesif.servlet.jersey.example; + +import com.moesif.servlet.MoesifConfiguration; +import com.moesif.servlet.MoesifFilter; + +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebFilter; +import javax.servlet.annotation.WebInitParam; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.logging.Logger; + +@WebFilter( + urlPatterns ="/*", + initParams = { + @WebInitParam(name = "application-id", + value = "Your Moesif Application Id"), + @WebInitParam(name = "logBody", value = "true") + } +) +public class MoesifServletFilter extends MoesifFilter { + Logger log = Logger.getLogger(MoesifServletFilter.class.getName()); + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + setDebug(true); + + // Use custom logic to determine User ID and other event metadata from requests + MoesifConfiguration config = new MoesifConfiguration() { + @Override + public String identifyUser(HttpServletRequest request, HttpServletResponse response) { + return "demo-user"; + } + + @Override + public String identifyCompany(HttpServletRequest request, HttpServletResponse response) { + return "demo-company"; + } + + @Override + public String getSessionToken(HttpServletRequest request, HttpServletResponse response) { + return "demo-session"; + } + + @Override + public String getApiVersion(HttpServletRequest request, HttpServletResponse response) { + return "1.0.0"; + } + }; + setConfigure(config); + super.init(filterConfig); + log.info("MoesifFilter initialized with " + this.getAPI()); + } +} \ No newline at end of file diff --git a/jersey-servlet-example/src/main/java/com/moesif/servlet/jersey/example/SubscriptionsServlet.java b/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/SubscriptionsServlet.java similarity index 100% rename from jersey-servlet-example/src/main/java/com/moesif/servlet/jersey/example/SubscriptionsServlet.java rename to jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/SubscriptionsServlet.java diff --git a/jersey-servlet-example/src/main/java/com/moesif/servlet/jersey/example/UsersServlet.java b/jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/UsersServlet.java similarity index 100% rename from jersey-servlet-example/src/main/java/com/moesif/servlet/jersey/example/UsersServlet.java rename to jersey-servlet-example-annotations/src/main/java/com/moesif/servlet/jersey/example/UsersServlet.java diff --git a/jersey-servlet-example/src/main/webapp/file-upload.html b/jersey-servlet-example-annotations/src/main/webapp/file-upload.html similarity index 100% rename from jersey-servlet-example/src/main/webapp/file-upload.html rename to jersey-servlet-example-annotations/src/main/webapp/file-upload.html diff --git a/jersey-servlet-example/src/main/webapp/index.html b/jersey-servlet-example-annotations/src/main/webapp/index.html similarity index 100% rename from jersey-servlet-example/src/main/webapp/index.html rename to jersey-servlet-example-annotations/src/main/webapp/index.html diff --git a/jersey-servlet-example-xml/README.md b/jersey-servlet-example-xml/README.md new file mode 100644 index 0000000..6ebb37f --- /dev/null +++ b/jersey-servlet-example-xml/README.md @@ -0,0 +1,68 @@ +### Jersey Servlet + +There are multiple ways to run Jersey, as a Java Servlet or embedded with a Java NIO framework like Grizzly. This subsection focuses on running Jersey as a Servlet. + +Edit the web.xml file to add your application id that you obtained from your Moesif Account. + +```xml + + MoesifFilter + com.moesif.servlet.MoesifFilter + + application-id + Your Moesif application id + + + debug + false + + + + MoesifFilter + /* + + +``` + +#### Running the Jersey Servlet example + +In order to run this example you will need to have Java 7+ and Maven installed. + +Before starting, check that your maven version is 3.0.x or above: + +```sh +mvn -v +``` + +1. Clone the repository + + ```sh + git clone https://github.com/Moesif/moesif-servlet + cd moesif-servlet + ``` + +2. Update web.xml to use your own Moesif ApplicationId +(Register for an account on [moesif.com](https://www.moesif.com)) + + ```sh + vim jersey-servlet-example/src/main/webapp/WEB-INF/web.xml + ``` + +3. Compile the example + + ```sh + cd jersey-servlet-example + mvn clean install + ``` + +4. Run jersey-servlet-example + + ```sh + java -jar target/dependency/webapp-runner.jar target/*.war + ``` + +5. Go to `http://localhost:8080/api/demo` or the port that Tomcat is running on. + +In your Moesif Account, you should see API calls logged under API Analytics -> Live Event Stream. + +Shut it down manually with Ctrl-C. diff --git a/jersey-servlet-example/pom.xml b/jersey-servlet-example-xml/pom.xml similarity index 96% rename from jersey-servlet-example/pom.xml rename to jersey-servlet-example-xml/pom.xml index 2a56f66..4f5e5f0 100644 --- a/jersey-servlet-example/pom.xml +++ b/jersey-servlet-example-xml/pom.xml @@ -4,13 +4,13 @@ 4.0.0 com.moesif.servlet.jersey - jersey-servlet-example + jersey-servlet-example-xml war 1.3.2 - jersey-servlet-example + jersey-servlet-example-xml - jersey-servlet-example + jersey-servlet-example-xml org.apache.maven.plugins diff --git a/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/CompaniesServlet.java b/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/CompaniesServlet.java new file mode 100644 index 0000000..03f5d78 --- /dev/null +++ b/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/CompaniesServlet.java @@ -0,0 +1,83 @@ +package com.moesif.servlet.jersey.example; + +import javax.annotation.PostConstruct; +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.Configuration; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import com.moesif.api.MoesifAPIClient; +import com.moesif.api.APIHelper; +import com.moesif.api.controllers.APIController; +import com.moesif.api.models.CompanyModel; +import com.moesif.api.models.CompanyBuilder; +import com.moesif.api.models.CampaignModel; +import com.moesif.api.models.CampaignBuilder; + +/** + * Root resource (exposed at "companies" path) + */ +@Path("companies/{id}") +public class CompaniesServlet { + + private APIController apiClient; + + @Context + private Configuration config; + + @PostConstruct + public void init() { + String applicationId = (String) config.getProperty("application-id"); + apiClient = new MoesifAPIClient(applicationId).getAPI(); + } + + /** + * Method handling HTTP POST requests. The returned object will be sent + * to the client as "application/json" media type. + * + * @return Response that will be returned as an application/json. + */ + @POST + @Produces(MediaType.APPLICATION_JSON) + public Response updateCompany(@PathParam("id") String id) throws Throwable { + + // Campaign object is optional, but useful if you want to track ROI of acquisition channels + // See https://www.moesif.com/docs/api#update-a-company for campaign schema + CampaignModel campaign = new CampaignBuilder() + .utmSource("google") + .utmCampaign("cpc") + .utmMedium("adwords") + .utmTerm("api+tooling") + .utmContent("landing") + .build(); + + // Only companyId is required + // metadata can be any custom object + CompanyModel company = new CompanyBuilder() + .companyId("67890") + .companyDomain("acmeinc.com") // If set, Moesif will enrich your profiles with publicly available info + .campaign(campaign) + .metadata(APIHelper.deserialize("{" + + "\"org_name\": \"Acme, Inc\"," + + "\"plan_name\": \"Free\"," + + "\"deal_stage\": \"Lead\"," + + "\"mrr\": 24000," + + "\"demographics\": {" + + "\"alexa_ranking\": 500000," + + "\"employee_count\": 47" + + "}" + + "}")) + .build(); + + apiClient.updateCompany(company); + + return Response.status(Status.CREATED).entity("{" + + "\"updated_company\": true" + + "}").build(); + + } +} diff --git a/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/FileUpload.java b/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/FileUpload.java new file mode 100644 index 0000000..c9ab35f --- /dev/null +++ b/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/FileUpload.java @@ -0,0 +1,55 @@ +package com.moesif.servlet.jersey.example; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.util.List; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.glassfish.jersey.media.multipart.BodyPartEntity; +import org.glassfish.jersey.media.multipart.FormDataBodyPart; +import org.glassfish.jersey.media.multipart.FormDataContentDisposition; +import org.glassfish.jersey.media.multipart.FormDataMultiPart; +import org.glassfish.jersey.media.multipart.FormDataParam; + + +@Path("upload") +public class FileUpload { + + @Path("/file") + @POST + @Consumes(MediaType.MULTIPART_FORM_DATA) + public Response uploadFile(@DefaultValue("") @FormDataParam("tags") String tags, + @FormDataParam("file") InputStream file, + @FormDataParam("file") FormDataContentDisposition fileDisposition) { + + String fileName = fileDisposition.getFileName(); + + saveFile(file, fileName); + + String fileDetails = "File saved at /tmp/" + fileName + " with tags "+ tags; + + System.out.println(fileDetails); + + return Response.ok(fileDetails).build(); + } + + private void saveFile(InputStream file, String name) { + try { + /* Change directory path */ + java.nio.file.Path path = FileSystems.getDefault().getPath("/tmp/" + name); + /* Save InputStream as file */ + Files.copy(file, path); + } catch (IOException ie) { + ie.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/JerseyDemo.java b/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/JerseyDemo.java new file mode 100644 index 0000000..9287c8f --- /dev/null +++ b/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/JerseyDemo.java @@ -0,0 +1,54 @@ +package com.moesif.servlet.jersey.example; + +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +/** + * Root resource (exposed at "demo" path) + */ +@Path("demo") +public class JerseyDemo { + + /** + * Method handling HTTP GET requests. The returned object will be sent + * to the client as "application/json" media type. + * + * @return String that will be returned as a application/json response. + */ + @GET + @Produces(MediaType.APPLICATION_JSON) + public String getIt() { + return "[" + + "{" + + "\"field_b\": \"value1\"" + + "}," + + "{" + + "\"field_b\": \"value2\"" + + "}," + + "{" + + "\"field_b\": \"value3\"" + + "}" + + "]"; + } + + /** + * Method handling HTTP POST requests. The returned object will be sent + * to the client as "application/json" media type. + * + * @return String that will be returned as a application/json response. + */ + @POST + @Produces(MediaType.APPLICATION_JSON) + public Response postIt() { + return Response.status(Response.Status.CREATED).entity("{" + + "\"field_a\": {" + + "\"id\": 123456," + + "\"msg\": \"Hello World.\"" + + "}" + + "}").build(); + } +} diff --git a/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/SubscriptionsServlet.java b/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/SubscriptionsServlet.java new file mode 100644 index 0000000..c3f4bb3 --- /dev/null +++ b/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/SubscriptionsServlet.java @@ -0,0 +1,73 @@ +package com.moesif.servlet.jersey.example; + +import java.util.Date; + +import javax.annotation.PostConstruct; +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.Configuration; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import com.moesif.api.MoesifAPIClient; +import com.moesif.api.APIHelper; +import com.moesif.api.controllers.APIController; +import com.moesif.api.models.SubscriptionModel; +import com.moesif.api.models.SubscriptionBuilder; + +/** + * Root resource (exposed at "subscriptions" path) + */ +@Path("subscriptions/{id}") +public class SubscriptionsServlet { + + private APIController apiClient; + + @Context + private Configuration config; + + @PostConstruct + public void init() { + String applicationId = (String) config.getProperty("application-id"); + apiClient = new MoesifAPIClient(applicationId).getAPI(); + } + + /** + * Method handling HTTP POST requests. The returned object will be sent + * to the client as "application/json" media type. + * + * @return Response that will be returned as an application/json. + */ + @POST + @Produces(MediaType.APPLICATION_JSON) + public Response updateSubscription(@PathParam("id") String id) throws Throwable { + + // Only subscriptionId, companyId, and status are required + // metadata can be any custom object + SubscriptionModel subscription = new SubscriptionBuilder() + .subscriptionId("sub_12345") + .companyId("67890") + .currentPeriodStart(new Date()) + .currentPeriodEnd(new Date()) + .status("active") + .metadata(APIHelper.deserialize("{" + + "\"email\": \"johndoe@acmeinc.com\"," + + "\"string_field\": \"value_1\"," + + "\"number_field\": 0," + + "\"object_field\": {" + + "\"field_1\": \"value_1\"," + + "\"field_2\": \"value_2\"" + + "}" + + "}")) + .build(); + + apiClient.updateSubscription(subscription); + + return Response.status(Status.CREATED).entity("{" + + "\"updated_company\": true" + + "}").build(); + } +} diff --git a/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/UsersServlet.java b/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/UsersServlet.java new file mode 100644 index 0000000..41adfc0 --- /dev/null +++ b/jersey-servlet-example-xml/src/main/java/com/moesif/servlet/jersey/example/UsersServlet.java @@ -0,0 +1,84 @@ +package com.moesif.servlet.jersey.example; + +import javax.annotation.PostConstruct; +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.Configuration; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import com.moesif.api.MoesifAPIClient; +import com.moesif.api.APIHelper; +import com.moesif.api.controllers.APIController; +import com.moesif.api.models.UserModel; +import com.moesif.api.models.UserBuilder; +import com.moesif.api.models.CampaignModel; +import com.moesif.api.models.CampaignBuilder; +import com.moesif.servlet.MoesifFilter; + +/** + * Root resource (exposed at "users" path) + */ +@Path("users/{id}") +public class UsersServlet { + + private APIController apiClient; + + @Context + private Configuration config; + + @PostConstruct + public void init() { + String applicationId = (String) config.getProperty("application-id"); + apiClient = new MoesifAPIClient(applicationId).getAPI(); + } + + /** + * Method handling HTTP POST requests. The returned object will be sent + * to the client as "application/json" media type. + * + * @return Response that will be returned as an application/json. + */ + @POST + @Produces(MediaType.APPLICATION_JSON) + public Response updateUser(@PathParam("id") String id) throws Throwable { + + // Campaign object is optional, but useful if you want to track ROI of acquisition channels + // See https://www.moesif.com/docs/api#users for campaign schema + CampaignModel campaign = new CampaignBuilder() + .utmSource("google") + .utmCampaign("cpc") + .utmMedium("adwords") + .utmTerm("api+tooling") + .utmContent("landing") + .build(); + + // Only userId is required + // metadata can be any custom object + UserModel user = new UserBuilder() + .userId(id) + .companyId("67890") // If set, associate user with a company object + .campaign(campaign) + .metadata(APIHelper.deserialize("{" + + "\"email\": \"johndoe@acmeinc.com\"," + + "\"first_name\": \"John\"," + + "\"last_name\": \"Doe\"," + + "\"title\": \"Software Engineer\"," + + "\"sales_info\": {" + + "\"stage\": \"Customer\"," + + "\"lifetime_value\": 24000," + + "\"account_owner\": \"mary@contoso.com\"" + + "}" + + "}")) + .build(); + + apiClient.updateUser(user); + + return Response.status(Status.CREATED).entity("{" + + "\"updated_user\": true" + + "}").build(); + } +} diff --git a/jersey-servlet-example/src/main/webapp/WEB-INF/web.xml b/jersey-servlet-example-xml/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from jersey-servlet-example/src/main/webapp/WEB-INF/web.xml rename to jersey-servlet-example-xml/src/main/webapp/WEB-INF/web.xml diff --git a/jersey-servlet-example-xml/src/main/webapp/file-upload.html b/jersey-servlet-example-xml/src/main/webapp/file-upload.html new file mode 100644 index 0000000..fd6341b --- /dev/null +++ b/jersey-servlet-example-xml/src/main/webapp/file-upload.html @@ -0,0 +1,15 @@ + + + + + Upload File Test + + +

Upload file

+
+

+

+ +
+ + \ No newline at end of file diff --git a/jersey-servlet-example-xml/src/main/webapp/index.html b/jersey-servlet-example-xml/src/main/webapp/index.html new file mode 100644 index 0000000..a064b45 --- /dev/null +++ b/jersey-servlet-example-xml/src/main/webapp/index.html @@ -0,0 +1,8 @@ + + +

Jersey RESTful Web Application!

+

Jersey resource +

Visit Project Jersey website + for more information on Jersey! + +