示例#1
0
 public void writeTo(
     T obj,
     Class<?> cls,
     Type genericType,
     Annotation[] anns,
     MediaType m,
     MultivaluedMap<String, Object> headers,
     OutputStream os)
     throws IOException {
   try {
     String encoding = HttpUtils.getSetEncoding(m, headers, null);
     if (InjectionUtils.isSupportedCollectionOrArray(cls)) {
       marshalCollection(cls, obj, genericType, encoding, os, m, anns);
     } else {
       Object actualObject = checkAdapter(obj, cls, anns, true);
       Class<?> actualClass =
           obj != actualObject || cls.isInterface() ? actualObject.getClass() : cls;
       marshal(actualObject, actualClass, genericType, encoding, os, m, anns);
     }
   } catch (JAXBException e) {
     handleJAXBException(e, false);
   } catch (WebApplicationException e) {
     throw e;
   } catch (Exception e) {
     LOG.warning(ExceptionUtils.getStackTrace(e));
     throw ExceptionUtils.toInternalServerErrorException(e, null);
   }
 }
示例#2
0
  protected XMLStreamReader getStreamReader(InputStream is, Class<?> type, MediaType mt) {
    MessageContext mc = getContext();
    XMLStreamReader reader = mc != null ? mc.getContent(XMLStreamReader.class) : null;
    if (reader == null && mc != null) {
      XMLInputFactory factory = (XMLInputFactory) mc.get(XMLInputFactory.class.getName());
      if (factory != null) {
        try {
          reader = factory.createXMLStreamReader(is);
        } catch (XMLStreamException e) {
          throw ExceptionUtils.toInternalServerErrorException(
              new RuntimeException("Can not create XMLStreamReader", e), null);
        }
      }
    }

    if (reader == null && is == null) {
      reader = getStreamHandlerFromCurrentMessage(XMLStreamReader.class);
    }

    reader = createTransformReaderIfNeeded(reader, is);
    reader = createDepthReaderIfNeeded(reader, is);
    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
      return new JAXBCollectionWrapperReader(TransformUtils.createNewReaderIfNeeded(reader, is));
    } else {
      return reader;
    }
  }
示例#3
0
 protected void addMatrixQueryParamsToBuilder(
     UriBuilder ub, String paramName, ParameterType pt, Annotation[] anns, Object... pValues) {
   if (pt != ParameterType.MATRIX && pt != ParameterType.QUERY) {
     throw new IllegalArgumentException(
         "This method currently deal " + "with matrix and query parameters only");
   }
   if (!"".equals(paramName)) {
     if (pValues != null && pValues.length > 0) {
       for (Object pValue : pValues) {
         if (InjectionUtils.isSupportedCollectionOrArray(pValue.getClass())) {
           Collection<?> c =
               pValue.getClass().isArray()
                   ? Arrays.asList((Object[]) pValue)
                   : (Collection<?>) pValue;
           for (Iterator<?> it = c.iterator(); it.hasNext(); ) {
             convertMatrixOrQueryToBuilder(ub, paramName, it.next(), pt, anns);
           }
         } else {
           convertMatrixOrQueryToBuilder(ub, paramName, pValue, pt, anns);
         }
       }
     } else {
       addMatrixOrQueryToBuilder(ub, paramName, pt, pValues);
     }
   } else {
     Object pValue = pValues[0];
     MultivaluedMap<String, Object> values = InjectionUtils.extractValuesFromBean(pValue, "");
     for (Map.Entry<String, List<Object>> entry : values.entrySet()) {
       for (Object v : entry.getValue()) {
         convertMatrixOrQueryToBuilder(ub, entry.getKey(), v, pt, anns);
       }
     }
   }
 }
示例#4
0
 private void addFormValue(
     MultivaluedMap<String, String> form, String name, Object pValue, Annotation[] anns) {
   if (pValue != null) {
     if (InjectionUtils.isSupportedCollectionOrArray(pValue.getClass())) {
       Collection<?> c =
           pValue.getClass().isArray() ? Arrays.asList((Object[]) pValue) : (Collection<?>) pValue;
       for (Iterator<?> it = c.iterator(); it.hasNext(); ) {
         FormUtils.addPropertyToForm(form, name, convertParamValue(it.next(), anns));
       }
     } else {
       FormUtils.addPropertyToForm(
           form, name, name.isEmpty() ? pValue : convertParamValue(pValue, anns));
     }
   }
 }
  @Override
  public boolean isWriteable(Class<?> type, Type genericType, Annotation[] anns, MediaType mt) {
    // JAXB support is required
    if (!super.isWriteable(type, genericType, anns, mt)) {
      return false;
    }
    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
      return supportJaxbOnly;
    }

    // if the user has set the list of out classes and a given class
    // is in that list then it can only be handled by the template
    if (outClassCanBeHandled(type.getName()) || outClassesToHandle == null && !supportJaxbOnly) {
      return outTemplatesAvailable(type, anns, mt);
    } else {
      return supportJaxbOnly;
    }
  }
示例#6
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;
  }