Esempio n. 1
0
  protected void marshalCollection(
      Class<?> originalCls,
      Object collection,
      Type genericType,
      String enc,
      OutputStream os,
      MediaType m,
      Annotation[] anns)
      throws Exception {

    Class<?> actualClass = InjectionUtils.getActualType(genericType);
    actualClass = getActualType(actualClass, genericType, anns);

    Collection<?> c =
        originalCls.isArray() ? Arrays.asList((Object[]) collection) : (Collection<?>) collection;

    Iterator<?> it = c.iterator();

    Object firstObj = it.hasNext() ? it.next() : null;

    QName qname = null;
    if (firstObj instanceof JAXBElement) {
      JAXBElement<?> el = (JAXBElement<?>) firstObj;
      qname = el.getName();
      actualClass = el.getDeclaredType();
    } else {
      qname = getCollectionWrapperQName(actualClass, genericType, firstObj, true);
    }
    if (qname == null) {
      String message =
          new org.apache.cxf.common.i18n.Message("NO_COLLECTION_ROOT", BUNDLE).toString();
      throw new WebApplicationException(Response.serverError().entity(message).build());
    }

    StringBuilder pi = new StringBuilder();
    pi.append(XML_PI_START + (enc == null ? "UTF-8" : enc) + "\"?>");
    os.write(pi.toString().getBytes());
    String startTag = null;
    String endTag = null;

    if (qname.getNamespaceURI().length() > 0) {
      String prefix = nsPrefixes.get(qname.getNamespaceURI());
      if (prefix == null) {
        prefix = "ns1";
      }
      startTag =
          "<"
              + prefix
              + ":"
              + qname.getLocalPart()
              + " xmlns:"
              + prefix
              + "=\""
              + qname.getNamespaceURI()
              + "\">";
      endTag = "</" + prefix + ":" + qname.getLocalPart() + ">";
    } else {
      startTag = "<" + qname.getLocalPart() + ">";
      endTag = "</" + qname.getLocalPart() + ">";
    }
    os.write(startTag.getBytes());
    if (firstObj != null) {
      XmlJavaTypeAdapter adapter =
          org.apache.cxf.jaxrs.utils.JAXBUtils.getAdapter(firstObj.getClass(), anns);
      marshalCollectionMember(
          JAXBUtils.useAdapter(firstObj, adapter, true),
          actualClass,
          genericType,
          enc,
          os,
          anns,
          m,
          qname.getNamespaceURI());
      while (it.hasNext()) {
        marshalCollectionMember(
            JAXBUtils.useAdapter(it.next(), adapter, true),
            actualClass,
            genericType,
            enc,
            os,
            anns,
            m,
            qname.getNamespaceURI());
      }
    }
    os.write(endTag.getBytes());
  }
Esempio n. 2
0
  public T readFrom(
      Class<T> type,
      Type genericType,
      Annotation[] anns,
      MediaType mt,
      MultivaluedMap<String, String> headers,
      InputStream is)
      throws IOException {
    if (isPayloadEmpty(headers)) {
      if (AnnotationUtils.getAnnotation(anns, Nullable.class) != null) {
        return null;
      } else {
        reportEmptyContentLength();
      }
    }

    XMLStreamReader reader = null;
    Unmarshaller unmarshaller = null;
    try {

      boolean isCollection = InjectionUtils.isSupportedCollectionOrArray(type);
      Class<?> theGenericType = isCollection ? InjectionUtils.getActualType(genericType) : type;
      Class<?> theType = getActualType(theGenericType, genericType, anns);

      unmarshaller = createUnmarshaller(theType, genericType, isCollection);
      addAttachmentUnmarshaller(unmarshaller);
      Object response = null;
      if (JAXBElement.class.isAssignableFrom(type)
          || !isCollection
              && (unmarshalAsJaxbElement
                  || jaxbElementClassMap != null
                      && jaxbElementClassMap.containsKey(theType.getName()))) {
        reader = getStreamReader(is, type, mt);
        reader = TransformUtils.createNewReaderIfNeeded(reader, is);
        if (JAXBElement.class.isAssignableFrom(type) && type == theType) {
          response = unmarshaller.unmarshal(reader);
        } else {
          response = unmarshaller.unmarshal(reader, theType);
        }
      } else {
        response = doUnmarshal(unmarshaller, type, is, anns, mt);
      }
      if (response instanceof JAXBElement && !JAXBElement.class.isAssignableFrom(type)) {
        response = ((JAXBElement<?>) response).getValue();
      }
      if (isCollection) {
        response =
            ((CollectionWrapper) response)
                .getCollectionOrArray(
                    unmarshaller,
                    theType,
                    type,
                    genericType,
                    org.apache.cxf.jaxrs.utils.JAXBUtils.getAdapter(theGenericType, anns));
      } else {
        response = checkAdapter(response, type, anns, false);
      }
      return type.cast(response);

    } catch (JAXBException e) {
      handleJAXBException(e, true);
    } catch (DepthExceededStaxException e) {
      throw ExceptionUtils.toWebApplicationException(null, JAXRSUtils.toResponse(413));
    } catch (WebApplicationException e) {
      throw e;
    } catch (Exception e) {
      LOG.warning(ExceptionUtils.getStackTrace(e));
      throw ExceptionUtils.toBadRequestException(e, null);
    } finally {
      try {
        StaxUtils.close(reader);
      } catch (XMLStreamException e) {
        // Ignore
      }
      JAXBUtils.closeUnmarshaller(unmarshaller);
    }
    // unreachable
    return null;
  }