public void writeTo(
      Object entry,
      Class<?> type,
      Type genericType,
      Annotation[] annotations,
      MediaType mediaType,
      MultivaluedMap<String, Object> httpHeaders,
      OutputStream entityStream)
      throws IOException, WebApplicationException {
    JAXBContextFinder finder = getFinder(mediaType);
    if (finder == null) {
      throw new JAXBMarshalException("Unable to find JAXBContext for media type: " + mediaType);
    }
    Class baseType = Types.getCollectionBaseType(type, genericType);
    try {
      JAXBContext ctx =
          finder.findCacheContext(mediaType, annotations, JaxbCollection.class, baseType);
      JaxbCollection col = new JaxbCollection();
      if (type.isArray()) {
        Object[] array = (Object[]) entry;
        for (Object obj : array) {
          col.getValue().add(obj);
        }
      } else {
        Collection collection = (Collection) entry;
        for (Object obj : collection) col.getValue().add(obj);
      }

      String element = "collection";
      String namespaceURI = "";
      String prefix = "";

      Wrapped wrapped = FindAnnotation.findAnnotation(annotations, Wrapped.class);
      if (wrapped != null) {
        element = wrapped.element();
        namespaceURI = wrapped.namespace();
        prefix = wrapped.prefix();
      }

      JAXBElement<JaxbCollection> collection =
          new JAXBElement<JaxbCollection>(
              new QName(namespaceURI, element, prefix), JaxbCollection.class, col);
      Marshaller marshaller = ctx.createMarshaller();
      AbstractJAXBProvider.decorateMarshaller(baseType, annotations, mediaType, marshaller);
      marshaller.marshal(collection, entityStream);
    } catch (JAXBException e) {
      throw new JAXBMarshalException(e);
    }
  }
Example #2
0
 public void setProduceMediaTypes(List<String> types) {
   super.setProduceMediaTypes(types);
 }
Example #3
0
 public void setConsumeMediaTypes(List<String> types) {
   super.setConsumeMediaTypes(types);
 }
Example #4
0
 public void setEnableBuffering(boolean enableBuf) {
   super.setEnableBuffering(enableBuf);
 }
Example #5
0
 @Context
 public void setMessageContext(MessageContext mc) {
   super.setContext(mc);
 }
  public Object readFrom(
      Class<Object> type,
      Type genericType,
      Annotation[] annotations,
      MediaType mediaType,
      MultivaluedMap<String, String> httpHeaders,
      InputStream entityStream)
      throws IOException, WebApplicationException {
    JAXBContextFinder finder = getFinder(mediaType);
    if (finder == null) {
      throw new JAXBUnmarshalException("Unable to find JAXBContext for media type: " + mediaType);
    }
    Class baseType = Types.getCollectionBaseType(type, genericType);
    JaxbCollection col = null;
    try {
      JAXBElement<JaxbCollection> ele = null;

      if (suppressExpandEntityExpansion()) {
        SAXSource source = new SAXSource(new InputSource(entityStream));
        JAXBContext ctx = finder.findCachedContext(JaxbCollection.class, mediaType, annotations);
        Unmarshaller unmarshaller = ctx.createUnmarshaller();
        unmarshaller = new ExternalEntityUnmarshaller(unmarshaller);
        ele = unmarshaller.unmarshal(source, JaxbCollection.class);
      } else {
        StreamSource source = new StreamSource(entityStream);
        JAXBContext ctx = finder.findCachedContext(JaxbCollection.class, mediaType, annotations);
        ele = ctx.createUnmarshaller().unmarshal(source, JaxbCollection.class);
      }

      Wrapped wrapped = FindAnnotation.findAnnotation(annotations, Wrapped.class);
      if (wrapped != null) {
        if (!wrapped.element().equals(ele.getName().getLocalPart())) {
          throw new JAXBUnmarshalException(
              "Collection wrapping failed, expected root element name of "
                  + wrapped.element()
                  + " got "
                  + ele.getName().getLocalPart());
        }
        if (!wrapped.namespace().equals(ele.getName().getNamespaceURI())) {
          throw new JAXBUnmarshalException(
              "Collection wrapping failed, expect namespace of "
                  + wrapped.namespace()
                  + " got "
                  + ele.getName().getNamespaceURI());
        }
      }

      col = ele.getValue();
    } catch (JAXBException e) {
      throw new JAXBUnmarshalException(e);
    }

    try {
      JAXBContext ctx = finder.findCachedContext(baseType, mediaType, null);
      Unmarshaller unmarshaller = ctx.createUnmarshaller();
      unmarshaller =
          AbstractJAXBProvider.decorateUnmarshaller(baseType, annotations, mediaType, unmarshaller);
      if (type.isArray()) {
        Object array = Array.newInstance(baseType, col.getValue().size());
        for (int i = 0; i < col.getValue().size(); i++) {
          Element val = (Element) col.getValue().get(i);
          Array.set(array, i, unmarshaller.unmarshal(val));
        }
        return array;
      } else {
        Collection outCol = null;
        if (type.isInterface()) {
          if (List.class.isAssignableFrom(type)) outCol = new ArrayList();
          else if (SortedSet.class.isAssignableFrom(type)) outCol = new TreeSet();
          else if (Set.class.isAssignableFrom(type)) outCol = new HashSet();
          else outCol = new ArrayList();
        } else {
          try {
            outCol = (Collection) type.newInstance();
          } catch (Exception e) {
            throw new JAXBUnmarshalException(e);
          }
        }
        for (Object obj : col.getValue()) {
          Element val = (Element) obj;
          outCol.add(unmarshaller.unmarshal(val));
        }
        return outCol;
      }
    } catch (JAXBException e) {
      throw new JAXBUnmarshalException(e);
    }
  }