Source code file content
arena / arena-http / src / main / java / com / kenai / puj / arena / http / PujResourceIdentifierResource.java
Size: 4595 bytes, 1 line
package com.kenai.puj.arena.http;
import java.io.IOException;
import java.text.ParseException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.ManagedBean;
import javax.ejb.EJB;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.ws.WebServiceException;
import com.kenai.puj.arena.model.entity.PujHomeworkEntity;
import com.kenai.puj.arena.model.entity.PujUserDetailsEntity;
import com.kenai.puj.arena.model.entity.facade.EntityFacadeConstants;
import com.kenai.puj.arena.model.entity.facade.PujHomeworkFacade;
import com.kenai.puj.arena.model.entity.facade.PujUserDetailsFacade;
import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataParam;
@Path("uri")
@ManagedBean
public class PujResourceIdentifierResource {
@EJB
private PujHomeworkFacade facade;
@EJB
private PujUserDetailsFacade userFacade;
@GET
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Collection<PujHomeworkEntity> readAll(
@QueryParam(EntityFacadeConstants.PARAM_START) @DefaultValue(EntityFacadeConstants.PARAM_START_DEFAULT_VALUE) int start,
@QueryParam(EntityFacadeConstants.PARAM_MAX) @DefaultValue(EntityFacadeConstants.PARAM_MAX_DEFAULT_VALUE) int max,
@QueryParam("acronym") String name,
@QueryParam("title") String title,
@QueryParam("institution") String institution) {
// return facade.readAllByCompetition(name, start, max);
Map<String, String> parameters = new HashMap<String, String>();
if (name != null) {
parameters.put("acronym", name);
}
return facade.findByCriteria(parameters, start, max);
}
@GET
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("{id}")
public PujHomeworkEntity read(@PathParam("id") String id) {
return facade.read(PujHomeworkEntity.class, id);
}
@DELETE
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("{id}")
public void delete(@PathParam("puj") String name, @PathParam("id") String id) {
facade.delete(PujHomeworkEntity.class, id);
}
/**
* <code>curl -v -H "Accept: application/json" -H "Content-Type: application/json" -X POST http://fgaucho.dyndns.org:8080/arena-http/PUJCE-08/homework/teste</code>
*
* @param name
* @param acronym
* @return
*/
@POST
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("{acronym}")
public PujHomeworkEntity create(@PathParam("puj") String name,
@PathParam("acronym") String acronym) {
throw new WebServiceException("not yet implemented");
/*
* String login = security.getUserPrincipal().getName();
* PujUserDetailsEntity user =
* userFacade.read(PujUserDetailsEntity.class, login); PujHomeworkEntity
* entity = new PujHomeworkEntity(); entity.setAcronym(acronym);
* entity.getAuthor().add(user); return facade.create(entity);
*/
}
@GET
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("{acronym}/author")
public Collection<PujUserDetailsEntity> readHomeworkStudents(
@PathParam("acronym") String homeworkAcronym,
@PathParam("puj") String pujName, @QueryParam("role") String role) {
return userFacade.readHomeworkAuthors(pujName, homeworkAcronym, role);
}
/** ------------ homework attachment --------------- */
@POST
@Path("{acronym}/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadAttachment(
@FormDataParam("file") List<FormDataBodyPart> parts,
@FormDataParam("submit") FormDataBodyPart submit)
throws IOException, ParseException {
System.out.println("XXXX: " + submit.getMediaType());
System.out.println("XXXX: "
+ submit.getHeaders().getFirst("Content-Type"));
for (FormDataBodyPart bp : parts) {
System.out.println(bp.getMediaType());
System.out.println(bp.getHeaders().get("Content-Disposition"));
System.out.println(bp.getParameterizedHeaders().getFirst(
"Content-Disposition").getParameters().get("name"));
bp.cleanup();
}
return Response.ok("file deleted").build();
}
@GET
@Path("{acronym}/file")
public byte[] downloadAttachment() {
throw new WebServiceException("not yet implemented");
}
@DELETE
@Path("{acronym}/file")
public Response deleteAttachment() {
return Response.ok("file deleted").build();
}
}





