Search in sources :

Example 26 with GenericEntity

use of javax.ws.rs.core.GenericEntity in project ORCID-Source by ORCID.

the class IdentifierApiServiceDelegatorTest method testviewIdentifierTypesWithLocale.

@Test
public void testviewIdentifierTypesWithLocale() {
    assertEquals(service.viewIdentifierTypes("es").getStatus(), 200);
    Response r = service.viewIdentifierTypes("es");
    @SuppressWarnings("unchecked") GenericEntity<List<IdentifierType>> types = (GenericEntity<List<IdentifierType>>) r.getEntity();
    Boolean found = false;
    for (IdentifierType t : types.getEntity()) {
        if (t.getName().equals("doi")) {
            assertEquals("doi: Identificador de objeto digital", t.getDescription());
            found = true;
        }
    }
    assertTrue("no description for DOI found", found);
}
Also used : Response(javax.ws.rs.core.Response) GenericEntity(javax.ws.rs.core.GenericEntity) ArrayList(java.util.ArrayList) List(java.util.List) IdentifierType(org.orcid.pojo.IdentifierType) Test(org.junit.Test)

Example 27 with GenericEntity

use of javax.ws.rs.core.GenericEntity in project jersey by jersey.

the class TestContainerRequest method setEntity.

void setEntity(final Object requestEntity, final MessageBodyWorkers workers) {
    final Object entity;
    final GenericType entityType;
    if (requestEntity instanceof GenericEntity) {
        entity = ((GenericEntity) requestEntity).getEntity();
        entityType = new GenericType(((GenericEntity) requestEntity).getType());
    } else {
        entity = requestEntity;
        entityType = new GenericType(requestEntity.getClass());
    }
    final byte[] entityBytes;
    if (entity != null) {
        final ByteArrayOutputStream output = new ByteArrayOutputStream();
        OutputStream stream = null;
        try {
            stream = workers.writeTo(entity, entity.getClass(), entityType.getType(), new Annotation[0], getMediaType(), new MultivaluedHashMap<String, Object>(getHeaders()), getPropertiesDelegate(), output, Collections.<WriterInterceptor>emptyList());
        } catch (final IOException | WebApplicationException ex) {
            LOGGER.log(Level.SEVERE, "Transforming entity to input stream failed.", ex);
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (final IOException e) {
                // ignore
                }
            }
        }
        entityBytes = output.toByteArray();
    } else {
        entityBytes = new byte[0];
    }
    setEntity(new ByteArrayInputStream(entityBytes));
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) GenericType(javax.ws.rs.core.GenericType) WebApplicationException(javax.ws.rs.WebApplicationException) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Annotation(java.lang.annotation.Annotation) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) GenericEntity(javax.ws.rs.core.GenericEntity)

Example 28 with GenericEntity

use of javax.ws.rs.core.GenericEntity in project jersey by jersey.

the class XmlMoxyTest method testJAXBListRepresentationMediaType.

@Test
public void testJAXBListRepresentationMediaType() {
    final WebTarget target = target("JAXBListResourceMediaType");
    Collection<JaxbBean> a = target.request().get(new GenericType<Collection<JaxbBean>>() {
    });
    Collection<JaxbBean> b = target.request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/foo+xml"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
    b = target.path("type").request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/foo+xml"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
    a = new LinkedList<>(a);
    b = target.path("queue").request().post(Entity.entity(new GenericEntity<Queue<JaxbBean>>((Queue<JaxbBean>) a) {
    }, "application/foo+xml"), new GenericType<Queue<JaxbBean>>() {
    });
    assertEquals(a, b);
    a = new HashSet<>(a);
    b = target.path("set").request().post(Entity.entity(new GenericEntity<Set<JaxbBean>>((Set<JaxbBean>) a) {
    }, "application/foo+xml"), new GenericType<Set<JaxbBean>>() {
    });
    final Comparator<JaxbBean> c = new Comparator<JaxbBean>() {

        @Override
        public int compare(final JaxbBean t, final JaxbBean t1) {
            return t.value.compareTo(t1.value);
        }
    };
    final TreeSet<JaxbBean> t1 = new TreeSet<>(c);
    final TreeSet<JaxbBean> t2 = new TreeSet<>(c);
    t1.addAll(a);
    t2.addAll(b);
    assertEquals(t1, t2);
    final Stack<JaxbBean> s = new Stack<>();
    s.addAll(a);
    b = target.path("stack").request().post(Entity.entity(new GenericEntity<Stack<JaxbBean>>(s) {
    }, "application/foo+xml"), new GenericType<Stack<JaxbBean>>() {
    });
    assertEquals(s, b);
    a = new MyArrayList<>(a);
    b = target.path("custom").request().post(Entity.entity(new GenericEntity<MyArrayList<JaxbBean>>((MyArrayList<JaxbBean>) a) {
    }, "application/foo+xml"), new GenericType<MyArrayList<JaxbBean>>() {
    });
    assertEquals(a, b);
}
Also used : GenericType(javax.ws.rs.core.GenericType) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) Comparator(java.util.Comparator) Stack(java.util.Stack) GenericEntity(javax.ws.rs.core.GenericEntity) TreeSet(java.util.TreeSet) Collection(java.util.Collection) WebTarget(javax.ws.rs.client.WebTarget) Queue(java.util.Queue) Test(org.junit.Test)

Example 29 with GenericEntity

use of javax.ws.rs.core.GenericEntity in project jersey by jersey.

the class EntityTypesTest method testJAXBListRepresentationMediaType.

@Test
public void testJAXBListRepresentationMediaType() {
    final WebTarget target = target("JAXBListResourceMediaType");
    Collection<JaxbBean> a = target.request().get(new GenericType<Collection<JaxbBean>>() {
    });
    Collection<JaxbBean> b = target.request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/foo+xml"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
    b = target.path("type").request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/foo+xml"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
    a = new LinkedList<>(a);
    b = target.path("queue").request().post(Entity.entity(new GenericEntity<Queue<JaxbBean>>((Queue<JaxbBean>) a) {
    }, "application/foo+xml"), new GenericType<Queue<JaxbBean>>() {
    });
    assertEquals(a, b);
    a = new HashSet<>(a);
    b = target.path("set").request().post(Entity.entity(new GenericEntity<Set<JaxbBean>>((Set<JaxbBean>) a) {
    }, "application/foo+xml"), new GenericType<Set<JaxbBean>>() {
    });
    final Comparator<JaxbBean> c = new Comparator<JaxbBean>() {

        @Override
        public int compare(final JaxbBean t, final JaxbBean t1) {
            return t.value.compareTo(t1.value);
        }
    };
    final TreeSet<JaxbBean> t1 = new TreeSet<>(c);
    final TreeSet<JaxbBean> t2 = new TreeSet<>(c);
    t1.addAll(a);
    t2.addAll(b);
    assertEquals(t1, t2);
    final Stack<JaxbBean> s = new Stack<>();
    s.addAll(a);
    b = target.path("stack").request().post(Entity.entity(new GenericEntity<Stack<JaxbBean>>(s) {
    }, "application/foo+xml"), new GenericType<Stack<JaxbBean>>() {
    });
    assertEquals(s, b);
    a = new MyArrayList<>(a);
    b = target.path("custom").request().post(Entity.entity(new GenericEntity<MyArrayList<JaxbBean>>((MyArrayList<JaxbBean>) a) {
    }, "application/foo+xml"), new GenericType<MyArrayList<JaxbBean>>() {
    });
    assertEquals(a, b);
}
Also used : GenericType(javax.ws.rs.core.GenericType) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Comparator(java.util.Comparator) Stack(java.util.Stack) GenericEntity(javax.ws.rs.core.GenericEntity) TreeSet(java.util.TreeSet) Collection(java.util.Collection) WebTarget(javax.ws.rs.client.WebTarget) Queue(java.util.Queue) Test(org.junit.Test)

Example 30 with GenericEntity

use of javax.ws.rs.core.GenericEntity in project jersey by jersey.

the class JsonMoxyTest method _testListOrArray.

@SuppressWarnings("unchecked")
public void _testListOrArray(final boolean isList, final MediaType mt) {
    final Object in = isList ? getJAXBElementList() : getJAXBElementArray();
    final GenericType gt = isList ? new GenericType<List<JAXBElement<String>>>() {
    } : new GenericType<JAXBElement<String>[]>() {
    };
    final WebTarget target = target(isList ? "JAXBElementListResource" : "JAXBElementArrayResource");
    final Object out = target.request(mt).post(Entity.entity(new GenericEntity(in, gt.getType()), mt), gt);
    final List<JAXBElement<String>> inList = isList ? ((List<JAXBElement<String>>) in) : Arrays.asList((JAXBElement<String>[]) in);
    final List<JAXBElement<String>> outList = isList ? ((List<JAXBElement<String>>) out) : Arrays.asList((JAXBElement<String>[]) out);
    assertEquals("Lengths differ", inList.size(), outList.size());
    for (int i = 0; i < inList.size(); i++) {
        assertEquals("Names of elements at index " + i + " differ", inList.get(i).getName(), outList.get(i).getName());
        assertEquals("Values of elements at index " + i + " differ", inList.get(i).getValue(), outList.get(i).getValue());
    }
}
Also used : GenericType(javax.ws.rs.core.GenericType) GenericEntity(javax.ws.rs.core.GenericEntity) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) JAXBElement(javax.xml.bind.JAXBElement)

Aggregations

GenericEntity (javax.ws.rs.core.GenericEntity)35 List (java.util.List)15 GenericType (javax.ws.rs.core.GenericType)15 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)14 WebTarget (javax.ws.rs.client.WebTarget)14 Collection (java.util.Collection)10 Produces (javax.ws.rs.Produces)9 Response (javax.ws.rs.core.Response)9 GET (javax.ws.rs.GET)7 Path (javax.ws.rs.Path)7 LinkedList (java.util.LinkedList)6 JAXBElement (javax.xml.bind.JAXBElement)5 IOException (java.io.IOException)4 Annotation (java.lang.annotation.Annotation)4 Comparator (java.util.Comparator)4 HashSet (java.util.HashSet)4 Queue (java.util.Queue)4 Set (java.util.Set)4 Stack (java.util.Stack)4