Search in sources :

Example 1 with Container

use of com.sun.jersey.samples.storageservice.Container in project enunciate by stoicflame.

the class ItemResource method putItem.

@PUT
public Response putItem(@Context HttpHeaders headers, byte[] data) {
    System.out.println("PUT ITEM " + container + " " + item);
    URI uri = uriInfo.getAbsolutePath();
    MediaType mimeType = headers.getMediaType();
    GregorianCalendar gc = new GregorianCalendar();
    gc.set(GregorianCalendar.MILLISECOND, 0);
    Item i = new Item(item, uri.toString(), mimeType.toString(), gc);
    String digest = computeDigest(data);
    i.setDigest(digest);
    Response r;
    if (!MemoryStore.MS.hasItem(container, item)) {
        r = Response.created(uri).build();
    } else {
        r = Response.noContent().build();
    }
    Item ii = MemoryStore.MS.createOrUpdateItem(container, i, data);
    if (ii == null) {
        // Create the container if one has not been created
        URI containerUri = uriInfo.getAbsolutePathBuilder().path("..").build().normalize();
        Container c = new Container(container, containerUri.toString());
        MemoryStore.MS.createContainer(c);
        i = MemoryStore.MS.createOrUpdateItem(container, i, data);
        if (i == null)
            throw new NotFoundException("Container not found");
    }
    return r;
}
Also used : Response(javax.ws.rs.core.Response) Item(com.sun.jersey.samples.storageservice.Item) Container(com.sun.jersey.samples.storageservice.Container) GregorianCalendar(java.util.GregorianCalendar) MediaType(javax.ws.rs.core.MediaType) NotFoundException(javax.ws.rs.NotFoundException) URI(java.net.URI) PUT(javax.ws.rs.PUT)

Example 2 with Container

use of com.sun.jersey.samples.storageservice.Container in project enunciate by stoicflame.

the class TestJerseyStorageService method testContainers.

/**
 * tests the containers.
 */
public void testContainers() throws Exception {
    WebTarget resource = getStorageResource();
    Response response = resource.path("containers").request("application/xml").get();
    assertEquals(200, response.getStatus());
    Containers containers = response.readEntity(Containers.class);
    assertTrue(containers.getContainer() == null || containers.getContainer().isEmpty());
    response = resource.path("containers").request("text/html").get();
    assertEquals(200, response.getStatus());
    response = resource.path("containers/one").request().get();
    assertEquals(404, response.getStatus());
    Container containerOne = new Container();
    response = resource.path("containers/one").request().put(Entity.entity(containerOne, "application/xml"));
    assertEquals(201, response.getStatus());
    response = resource.path("containers").request("application/xml").get();
    assertEquals(200, response.getStatus());
    containers = response.readEntity(Containers.class);
    assertEquals(1, containers.getContainer().size());
    response = resource.path("containers/one").request().get();
    assertEquals(200, response.getStatus());
    containerOne = response.readEntity(Container.class);
    assertEquals("one", containerOne.getName());
    assertNotNull(containerOne.getUri());
    assertTrue(containerOne.getItem() == null || containerOne.getItem().isEmpty());
    String stringItem = "here is a string that we want to store";
    response = resource.path("containers/one/string").request().put(Entity.entity(stringItem, "text/plain"));
    assertEquals(201, response.getStatus());
    response = resource.path("containers/one").request().get();
    assertEquals(200, response.getStatus());
    containerOne = response.readEntity(Container.class);
    assertEquals(1, containerOne.getItem().size());
    assertEquals("text/plain", containerOne.getItem("string").getMimeType());
    response = resource.path("containers/one/string").request().get();
    assertEquals(200, response.getStatus());
    assertEquals(stringItem, response.readEntity(String.class));
}
Also used : Response(javax.ws.rs.core.Response) Container(com.sun.jersey.samples.storageservice.Container) Containers(com.sun.jersey.samples.storageservice.Containers) WebTarget(javax.ws.rs.client.WebTarget)

Example 3 with Container

use of com.sun.jersey.samples.storageservice.Container in project enunciate by stoicflame.

the class TestJerseyStorageService method tearDown.

@Override
protected void tearDown() throws Exception {
    super.tearDown();
    WebTarget resource = getStorageResource();
    Response response = resource.path("containers").request("application/xml").get();
    if (200 == response.getStatus()) {
        Containers containers = response.readEntity(Containers.class);
        if (containers.getContainer() != null) {
            for (Container container : containers.getContainer()) {
                resource.path("containers/" + container.getName()).request().delete();
            }
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) Container(com.sun.jersey.samples.storageservice.Container) Containers(com.sun.jersey.samples.storageservice.Containers) WebTarget(javax.ws.rs.client.WebTarget)

Example 4 with Container

use of com.sun.jersey.samples.storageservice.Container in project enunciate by stoicflame.

the class ContainerResource method putContainer.

@PUT
@TypeHint(Container.class)
public Response putContainer() throws WebApplicationException {
    System.out.println("PUT CONTAINER " + container);
    URI uri = uriInfo.getAbsolutePath();
    Container c = new Container(container, uri.toString());
    Response r;
    if (!MemoryStore.MS.hasContainer(c)) {
        r = Response.created(uri).build();
    } else {
        r = Response.noContent().build();
    }
    MemoryStore.MS.createContainer(c);
    return r;
}
Also used : Response(javax.ws.rs.core.Response) Container(com.sun.jersey.samples.storageservice.Container) URI(java.net.URI) TypeHint(com.webcohesion.enunciate.metadata.rs.TypeHint)

Example 5 with Container

use of com.sun.jersey.samples.storageservice.Container in project enunciate by stoicflame.

the class ContainerResource method deleteContainer.

@DELETE
public void deleteContainer() {
    System.out.println("DELETE CONTAINER " + container);
    Container c = MemoryStore.MS.deleteContainer(container);
    if (c == null)
        throw new NotFoundException("Container not found");
}
Also used : Container(com.sun.jersey.samples.storageservice.Container)

Aggregations

Container (com.sun.jersey.samples.storageservice.Container)6 Response (javax.ws.rs.core.Response)4 Containers (com.sun.jersey.samples.storageservice.Containers)2 Item (com.sun.jersey.samples.storageservice.Item)2 URI (java.net.URI)2 WebTarget (javax.ws.rs.client.WebTarget)2 TypeHint (com.webcohesion.enunciate.metadata.rs.TypeHint)1 GregorianCalendar (java.util.GregorianCalendar)1 NotFoundException (javax.ws.rs.NotFoundException)1 PUT (javax.ws.rs.PUT)1 MediaType (javax.ws.rs.core.MediaType)1