Search in sources :

Example 21 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 22 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 23 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)

Example 24 with GenericEntity

use of javax.ws.rs.core.GenericEntity in project hadoop by apache.

the class AHSWebServices method getContainerLogMeta.

private Response getContainerLogMeta(ApplicationId appId, String appOwner, final String nodeId, final String containerIdStr, boolean emptyLocalContainerLogMeta) {
    try {
        List<ContainerLogMeta> containerLogMeta = LogToolUtils.getContainerLogMetaFromRemoteFS(conf, appId, containerIdStr, nodeId, appOwner);
        if (containerLogMeta.isEmpty()) {
            throw new NotFoundException("Can not get log meta for container: " + containerIdStr);
        }
        List<ContainerLogsInfo> containersLogsInfo = new ArrayList<>();
        for (ContainerLogMeta meta : containerLogMeta) {
            ContainerLogsInfo logInfo = new ContainerLogsInfo(meta, ContainerLogAggregationType.AGGREGATED);
            containersLogsInfo.add(logInfo);
        }
        if (emptyLocalContainerLogMeta) {
            ContainerLogMeta emptyMeta = new ContainerLogMeta(containerIdStr, "N/A");
            ContainerLogsInfo empty = new ContainerLogsInfo(emptyMeta, ContainerLogAggregationType.LOCAL);
            containersLogsInfo.add(empty);
        }
        GenericEntity<List<ContainerLogsInfo>> meta = new GenericEntity<List<ContainerLogsInfo>>(containersLogsInfo) {
        };
        ResponseBuilder response = Response.ok(meta);
        // Sending the X-Content-Type-Options response header with the value
        // nosniff will prevent Internet Explorer from MIME-sniffing a response
        // away from the declared content-type.
        response.header("X-Content-Type-Options", "nosniff");
        return response.build();
    } catch (Exception ex) {
        throw new WebApplicationException(ex);
    }
}
Also used : ContainerLogsInfo(org.apache.hadoop.yarn.server.webapp.dao.ContainerLogsInfo) WebApplicationException(javax.ws.rs.WebApplicationException) GenericEntity(javax.ws.rs.core.GenericEntity) ContainerLogMeta(org.apache.hadoop.yarn.logaggregation.ContainerLogMeta) ArrayList(java.util.ArrayList) NotFoundException(org.apache.hadoop.yarn.webapp.NotFoundException) List(java.util.List) ArrayList(java.util.ArrayList) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) WebApplicationException(javax.ws.rs.WebApplicationException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) NotFoundException(org.apache.hadoop.yarn.webapp.NotFoundException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException)

Example 25 with GenericEntity

use of javax.ws.rs.core.GenericEntity in project exhibitor by soabase.

the class UIResource method getAdditionalTabSpecs.

@Path("tabs")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAdditionalTabSpecs() {
    final AtomicInteger index = new AtomicInteger(0);
    List<UITabSpec> names = Lists.transform(tabs, new Function<UITab, UITabSpec>() {

        @Override
        public UITabSpec apply(UITab tab) {
            String base = tab.contentIsHtml() ? HTML_UI_TAB_BASE_URL : TEXT_UI_TAB_BASE_URL;
            return new UITabSpec(tab.getName(), base + index.getAndIncrement(), tab.contentIsHtml(), tab.getUITabType());
        }
    });
    // move out of Google's TransformingRandomAccessList
    names = Lists.newArrayList(names);
    GenericEntity<List<UITabSpec>> entity = new GenericEntity<List<UITabSpec>>(names) {
    };
    return Response.ok(entity).build();
}
Also used : UITabSpec(com.netflix.exhibitor.core.entities.UITabSpec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GenericEntity(javax.ws.rs.core.GenericEntity) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

GenericEntity (javax.ws.rs.core.GenericEntity)27 WebTarget (javax.ws.rs.client.WebTarget)13 GenericType (javax.ws.rs.core.GenericType)13 List (java.util.List)12 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)11 Collection (java.util.Collection)10 Produces (javax.ws.rs.Produces)9 GET (javax.ws.rs.GET)7 Path (javax.ws.rs.Path)7 Response (javax.ws.rs.core.Response)5 IOException (java.io.IOException)4 Comparator (java.util.Comparator)4 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 Queue (java.util.Queue)4 Set (java.util.Set)4 Stack (java.util.Stack)4 TreeSet (java.util.TreeSet)4 WebApplicationException (javax.ws.rs.WebApplicationException)4