WIP(ch9lab) add source for java-based todo-api app
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package com.redhat.training.rest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.ws.rs.container.ContainerRequestContext;
|
||||
import javax.ws.rs.container.ContainerRequestFilter;
|
||||
import javax.ws.rs.container.PreMatching;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
|
||||
@Provider
|
||||
@PreMatching
|
||||
public class CORSRequestFilter implements ContainerRequestFilter {
|
||||
|
||||
private final static Logger log = Logger.getLogger(CORSRequestFilter.class.getName());
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext requestCtx) throws IOException {
|
||||
log.fine("Executing REST request filter");
|
||||
|
||||
// When HttpMethod comes as OPTIONS, just acknowledge that it accepts...
|
||||
if (requestCtx.getRequest().getMethod().equals( "OPTIONS" )) {
|
||||
log.fine("HTTP Method (OPTIONS) - Detected!");
|
||||
|
||||
// Just send a OK signal back to the browser
|
||||
requestCtx.abortWith(Response.status(Response.Status.OK).build());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.redhat.training.rest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.ws.rs.container.ContainerRequestContext;
|
||||
import javax.ws.rs.container.ContainerResponseContext;
|
||||
import javax.ws.rs.container.ContainerResponseFilter;
|
||||
import javax.ws.rs.container.PreMatching;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
|
||||
@Provider
|
||||
@PreMatching
|
||||
public class CORSResponseFilter implements ContainerResponseFilter {
|
||||
|
||||
private final static Logger log = Logger.getLogger(CORSResponseFilter.class.getName());
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext requestCtx, ContainerResponseContext responseCtx) throws IOException {
|
||||
log.fine("Executing REST response filter");
|
||||
|
||||
MultivaluedMap<String, Object> headers = responseCtx.getHeaders();
|
||||
|
||||
headers.add("Access-Control-Allow-Origin", "*");
|
||||
headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS");
|
||||
headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
|
||||
//responseCtx.getHeaders().add( "Access-Control-Allow-Credentials", "true" );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.redhat.training.rest;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import com.redhat.training.model.Host;
|
||||
|
||||
@Stateless
|
||||
@Path("host")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public class HostService {
|
||||
|
||||
@GET
|
||||
public Host getHostInfo() throws UnknownHostException {
|
||||
InetAddress address = InetAddress.getLocalHost();
|
||||
Host host = new Host(address.getHostAddress(), address.getHostName());
|
||||
return host;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.redhat.training.rest;
|
||||
|
||||
import com.redhat.training.model.Item;
|
||||
import com.redhat.training.ui.PaginatedListWrapper;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.Application;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Stateless
|
||||
@Path("items")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public class ItemService extends Application {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
private Integer countItems() {
|
||||
Query query = entityManager.createQuery("SELECT COUNT(i.id) FROM Item i");
|
||||
return ((Long) query.getSingleResult()).intValue();
|
||||
}
|
||||
|
||||
private List<Item> findItems(int startPosition, int maxResults, String sortFields, String sortDirections) {
|
||||
TypedQuery<Item> query =
|
||||
entityManager.createQuery("SELECT i FROM Item i ORDER BY i." + sortFields + " " + sortDirections,
|
||||
Item.class);
|
||||
query.setFirstResult(startPosition);
|
||||
query.setMaxResults(maxResults);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
private PaginatedListWrapper findItems(PaginatedListWrapper wrapper) {
|
||||
wrapper.setTotalResults(countItems());
|
||||
int start = (wrapper.getCurrentPage() - 1) * wrapper.getPageSize();
|
||||
wrapper.setList(findItems(start,
|
||||
wrapper.getPageSize(),
|
||||
wrapper.getSortFields(),
|
||||
wrapper.getSortDirections()));
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public PaginatedListWrapper listItems(@DefaultValue("1")
|
||||
@QueryParam("page")
|
||||
Integer page,
|
||||
@DefaultValue("id")
|
||||
@QueryParam("sortFields")
|
||||
String sortFields,
|
||||
@DefaultValue("asc")
|
||||
@QueryParam("sortDirections")
|
||||
String sortDirections) {
|
||||
PaginatedListWrapper paginatedListWrapper = new PaginatedListWrapper();
|
||||
paginatedListWrapper.setCurrentPage(page);
|
||||
paginatedListWrapper.setSortFields(sortFields);
|
||||
paginatedListWrapper.setSortDirections(sortDirections);
|
||||
paginatedListWrapper.setPageSize(10);
|
||||
return findItems(paginatedListWrapper);
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("{id}")
|
||||
public Item getitem(@PathParam("id") Long id) {
|
||||
return entityManager.find(Item.class, id);
|
||||
}
|
||||
|
||||
@POST
|
||||
public Item saveItem(Item item) {
|
||||
if (item.getId() == null) {
|
||||
Item itemToSave = new Item();
|
||||
itemToSave.setDescription(item.getDescription());
|
||||
itemToSave.setDone(item.isDone());
|
||||
entityManager.persist(item);
|
||||
} else {
|
||||
Item itemToUpdate = getitem(item.getId());
|
||||
itemToUpdate.setDescription(item.getDescription());
|
||||
itemToUpdate.setDone(item.isDone());
|
||||
item = entityManager.merge(itemToUpdate);
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("{id}")
|
||||
public void deleteItem(@PathParam("id") Long id) {
|
||||
entityManager.remove(getitem(id));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.redhat.training.rest;
|
||||
|
||||
import javax.ws.rs.ApplicationPath;
|
||||
import javax.ws.rs.core.Application;
|
||||
|
||||
@ApplicationPath("/todo/api")
|
||||
public class RestApplication extends Application {
|
||||
}
|
||||
Reference in New Issue
Block a user