Esempio n. 1
0
 private void checkCachedStream(Message m, OutputStream osOriginal, boolean enabled)
     throws Exception {
   XMLStreamWriter writer = null;
   if (enabled) {
     writer = m.getContent(XMLStreamWriter.class);
   } else {
     writer = (XMLStreamWriter) m.get(XMLStreamWriter.class.getName());
   }
   if (writer instanceof CachingXmlEventWriter) {
     CachingXmlEventWriter cache = (CachingXmlEventWriter) writer;
     if (cache.getEvents().size() != 0) {
       XMLStreamWriter origWriter = null;
       try {
         origWriter = StaxUtils.createXMLStreamWriter(osOriginal);
         for (XMLEvent event : cache.getEvents()) {
           StaxUtils.writeEvent(event, origWriter);
         }
       } finally {
         StaxUtils.close(origWriter);
       }
     }
     m.setContent(XMLStreamWriter.class, null);
     return;
   }
   if (enabled) {
     OutputStream os = m.getContent(OutputStream.class);
     if (os != osOriginal && os instanceof CachedOutputStream) {
       CachedOutputStream cos = (CachedOutputStream) os;
       if (cos.size() != 0) {
         cos.writeCacheTo(osOriginal);
       }
     }
   }
 }
Esempio n. 2
0
 protected Object unmarshalFromInputStream(
     Unmarshaller unmarshaller, InputStream is, Annotation[] anns, MediaType mt)
     throws JAXBException {
   // Try to create the read before unmarshalling the stream
   XMLStreamReader xmlReader = null;
   try {
     if (is == null) {
       Reader reader = getStreamHandlerFromCurrentMessage(Reader.class);
       if (reader == null) {
         LOG.severe("No InputStream, Reader, or XMLStreamReader is available");
         throw ExceptionUtils.toInternalServerErrorException(null, null);
       }
       xmlReader = StaxUtils.createXMLStreamReader(reader);
     } else {
       xmlReader = StaxUtils.createXMLStreamReader(is);
     }
     configureReaderRestrictions(xmlReader);
     return unmarshaller.unmarshal(xmlReader);
   } finally {
     try {
       StaxUtils.close(xmlReader);
     } catch (XMLStreamException e) {
       // Ignore
     }
   }
 }
Esempio n. 3
0
 protected Object doUnmarshal(
     Unmarshaller unmarshaller, Class<?> type, InputStream is, Annotation[] anns, MediaType mt)
     throws JAXBException {
   XMLStreamReader reader = getStreamReader(is, type, mt);
   if (reader != null) {
     try {
       return unmarshalFromReader(unmarshaller, reader, anns, mt);
     } catch (JAXBException e) {
       throw e;
     } finally {
       try {
         StaxUtils.close(reader);
       } catch (XMLStreamException e) {
         // Ignore
       }
     }
   }
   return unmarshalFromInputStream(unmarshaller, is, anns, mt);
 }
 @Override
 public void close() {
   Reader transformedReader = null;
   try {
     super.flush();
     transformedReader = XSLTUtils.transform(xsltTemplate, cachedWriter.getReader());
     StaxUtils.copy(new StreamSource(transformedReader), origXWriter);
   } catch (XMLStreamException e) {
     throw new Fault("STAX_COPY", LOG, e, e.getMessage());
   } catch (IOException e) {
     throw new Fault("GET_CACHED_INPUT_STREAM", LOG, e, e.getMessage());
   } finally {
     try {
       if (transformedReader != null) {
         transformedReader.close();
       }
       cachedWriter.close();
       StaxUtils.close(origXWriter);
       super.close();
     } catch (Exception e) {
       LOG.warning("Cannot close stream after transformation: " + e.getMessage());
     }
   }
 }
Esempio n. 5
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;
  }