Skip to content

Commit

Permalink
Update chapter 4 examples to connect with Admin service from chapter 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kenfinnigan committed Dec 22, 2017
1 parent feb7093 commit 1ac4ee6
Show file tree
Hide file tree
Showing 31 changed files with 1,265 additions and 301 deletions.
24 changes: 21 additions & 3 deletions chapter4/time/pom.xml → chapter4/admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>chapter4-time</artifactId>
<name>Chapter 4: Time Microservice</name>
<artifactId>chapter4-admin</artifactId>
<name>Chapter 4: Cayambe Admin Service</name>
<packaging>war</packaging>

<build>
<finalName>time</finalName>
<finalName>admin</finalName>

<plugins>
<plugin>
Expand Down Expand Up @@ -43,5 +43,23 @@
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>cdi</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jpa</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ejm.chapter4.admin;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
* @author Ken Finnigan
*/
@ApplicationPath("/admin")
public class AdminApplication extends Application {
}
24 changes: 24 additions & 0 deletions chapter4/admin/src/main/java/ejm/chapter4/admin/CORSFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ejm.chapter4.admin;

import java.io.IOException;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;

/**
* @author Ken Finnigan
*/
@Provider
public class CORSFilter implements ContainerResponseFilter {

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
responseContext.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
responseContext.getHeaders().add("Access-Control-Allow-Credentials", "true");
responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
responseContext.getHeaders().add("Access-Control-Max-Age", "1209600");
}
}
130 changes: 130 additions & 0 deletions chapter4/admin/src/main/java/ejm/chapter4/admin/CategoryResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package ejm.chapter4.admin;

import java.net.URI;
import java.util.Collection;

import javax.enterprise.context.ApplicationScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import ejm.chapter4.admin.model.Category;
import ejm.chapter4.admin.model.CategoryTree;

/**
* @author Ken Finnigan
*/
@Path("/")
@ApplicationScoped
public class CategoryResource {

@PersistenceContext(unitName = "AdminPU")
private EntityManager em;

@GET
@Path("/category")
@Produces(MediaType.APPLICATION_JSON)
public Collection<Category> all() throws Exception {
return em.createNamedQuery("Category.findAll", Category.class)
.getResultList();
}

@GET
@Path("/categorytree")
@Produces(MediaType.APPLICATION_JSON)
public CategoryTree tree() throws Exception {
return em.find(CategoryTree.class, 1);
}

@POST
@Path("/category")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response create(Category category) throws Exception {
if (category.getId() != null) {
return Response
.status(Response.Status.CONFLICT)
.entity("Unable to create Category, id was already set.")
.build();
}

try {
em.persist(category);
} catch (Exception e) {
return Response
.serverError()
.entity(e.getMessage())
.build();
}
return Response
.created(new URI(category.getId().toString()))
.build();
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/category/{categoryId}")
public Category get(@PathParam("categoryId") Integer categoryId) {
return em.find(Category.class, categoryId);
}

@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("/category/{categoryId}")
@Transactional
public Response remove(@PathParam("categoryId") Integer categoryId) throws Exception {
try {
Category entity = em.find(Category.class, categoryId);
em.remove(entity);
} catch (Exception e) {
return Response
.serverError()
.entity(e.getMessage())
.build();
}

return Response
.noContent()
.build();
}

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/category/{categoryId}")
@Transactional
public Response update(@PathParam("categoryId") Integer categoryId, Category category) throws Exception {
try {
Category entity = em.find(Category.class, categoryId);

if (null == entity) {
return Response
.status(Response.Status.NOT_FOUND)
.entity("Category with id of " + categoryId + " does not exist.")
.build();
}

em.merge(category);

return Response
.ok(category)
.build();
} catch (Exception e) {
return Response
.serverError()
.entity(e.getMessage())
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ejm.chapter4.admin;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

/**
* @author Ken Finnigan
*/
@Provider
public class ConfigureJacksonProvider implements ContextResolver<ObjectMapper> {

private final ObjectMapper mapper = new ObjectMapper()
.registerModule(new JavaTimeModule());

@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ejm.chapter4.admin.converter;

import java.sql.Timestamp;
import java.time.LocalDateTime;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

/**
* @author Ken Finnigan
*/
@Converter(autoApply = true)
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime attribute) {
return attribute == null ? null : Timestamp.valueOf(attribute);
}

@Override
public LocalDateTime convertToEntityAttribute(Timestamp dbData) {
return dbData == null ? null : dbData.toLocalDateTime();
}
}
Loading

0 comments on commit 1ac4ee6

Please sign in to comment.