-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update chapter 4 examples to connect with Admin service from chapter 2
- Loading branch information
1 parent
feb7093
commit 1ac4ee6
Showing
31 changed files
with
1,265 additions
and
301 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
11 changes: 11 additions & 0 deletions
11
chapter4/admin/src/main/java/ejm/chapter4/admin/AdminApplication.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 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
24
chapter4/admin/src/main/java/ejm/chapter4/admin/CORSFilter.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,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
130
chapter4/admin/src/main/java/ejm/chapter4/admin/CategoryResource.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,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(); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
chapter4/admin/src/main/java/ejm/chapter4/admin/ConfigureJacksonProvider.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,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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
chapter4/admin/src/main/java/ejm/chapter4/admin/converter/LocalDateTimeConverter.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 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(); | ||
} | ||
} |
Oops, something went wrong.