/* (non-Javadoc)
   * @see org.apache.cxf.interceptor.Interceptor#handleMessage(org.apache.cxf.message.Message)
   */
  public void handleMessage(Message message) {
    try {
      if (message != null) {

        List<Object> params = message.getContent(List.class); // parameter instances
        if (params.get(0) instanceof HasXsl) {

          OperationResourceInfo ori = message.getExchange().get(OperationResourceInfo.class);
          JAXBElementProvider provider =
              (JAXBElementProvider)
                  ProviderFactory.getInstance(message)
                      .createMessageBodyWriter(
                          ori.getMethodToInvoke().getReturnType(),
                          ori.getMethodToInvoke().getGenericReturnType(),
                          ori.getMethodToInvoke().getAnnotations(),
                          MediaType.APPLICATION_XML_TYPE,
                          message);
          Map<String, Object> properties = new HashMap<String, Object>();

          HasXsl hasXsl = (HasXsl) params.get(0);
          // System.out.println("Stylesheet params " + hasXsl.getClass() + " " + hasXsl.getXsl());
          if (hasXsl == null || hasXsl.getXsl().equalsIgnoreCase(Constants.NO_XSL)) {
            properties.put(XML_HEADERS, "<!-- no stylesheet -->");
          } else {
            properties.put(
                XML_HEADERS,
                "<?xml-stylesheet type='application/xml' href='" + hasXsl.getXsl() + "'?>");
          }
          provider.setMarshallerProperties(properties);
        }
      }
    } catch (Throwable x) {
      throw new WebApplicationException(Response.status(Status.BAD_REQUEST).build());
    }
  }
Exemple #2
0
  private static void checkJaxbType(
      Class<?> serviceClass,
      Class<?> type,
      Type genericType,
      ResourceTypes types,
      Annotation[] anns,
      MessageBodyWriter<?> jaxbWriter) {
    boolean isCollection = false;
    if (InjectionUtils.isSupportedCollectionOrArray(type)) {
      type = InjectionUtils.getActualType(genericType);
      isCollection = true;
    }
    if (type == Object.class && !(genericType instanceof Class)) {
      Type theType =
          InjectionUtils.processGenericTypeIfNeeded(serviceClass, Object.class, genericType);
      type = InjectionUtils.getActualType(theType);
    }
    if (type == null
        || InjectionUtils.isPrimitive(type)
        || JAXBElement.class.isAssignableFrom(type)
        || Response.class.isAssignableFrom(type)
        || type.isInterface()) {
      return;
    }

    MessageBodyWriter<?> writer = jaxbWriter;
    if (writer == null) {
      JAXBElementProvider<Object> defaultWriter = new JAXBElementProvider<Object>();
      defaultWriter.setMarshallAsJaxbElement(true);
      defaultWriter.setXmlTypeAsJaxbElementOnly(true);
      writer = defaultWriter;
    }
    if (writer.isWriteable(type, type, anns, MediaType.APPLICATION_XML_TYPE)) {
      types.getAllTypes().put(type, type);
      Class<?> genCls = InjectionUtils.getActualType(genericType);
      if (genCls != type
          && genCls != null
          && genCls != Object.class
          && !InjectionUtils.isSupportedCollectionOrArray(genCls)) {
        types.getAllTypes().put(genCls, genCls);
      }

      XMLName name = AnnotationUtils.getAnnotation(anns, XMLName.class);
      QName qname = name != null ? JAXRSUtils.convertStringToQName(name.value()) : null;
      if (isCollection) {
        types.getCollectionMap().put(type, qname);
      } else {
        types.getXmlNameMap().put(type, qname);
      }
    }
  }
 private void verifyJaxbProvider(List<?> providers) throws Exception {
   JAXBElementProvider<?> provider = null;
   for (Object o : providers) {
     if (o instanceof JAXBElementProvider) {
       provider = (JAXBElementProvider<?>) o;
     }
   }
   assertNotNull(provider);
   JAXBContext c1 = provider.getClassContext(Book.class);
   assertNotNull(c1);
   JAXBContext c2 = provider.getClassContext(SuperBook.class);
   assertSame(c1, c2);
   provider.clearContexts();
 }
  @Override
  protected void marshalToOutputStream(
      Marshaller ms, Object obj, OutputStream os, Annotation[] anns, MediaType mt)
      throws Exception {

    Templates t = createTemplates(getOutTemplates(anns, mt), outParamsMap, outProperties);
    if (t == null && supportJaxbOnly) {
      super.marshalToOutputStream(ms, obj, os, anns, mt);
      return;
    }
    TransformerHandler th = null;
    try {
      th = factory.newTransformerHandler(t);
    } catch (TransformerConfigurationException ex) {
      TemplatesImpl ti = (TemplatesImpl) t;
      th = factory.newTransformerHandler(ti.getTemplates());
      this.trySettingProperties(th, ti);
    }
    Result result = new StreamResult(os);
    if (systemId != null) {
      result.setSystemId(systemId);
    }
    th.setResult(result);

    if (getContext() == null) {
      th.startDocument();
    }
    ms.marshal(obj, th);
    if (getContext() == null) {
      th.endDocument();
    }
  }
 private XmlPath toXml(final JaxbVideosXml obj) throws Exception {
   final ByteArrayOutputStream baos = new ByteArrayOutputStream();
   xmlProvider.writeTo(
       obj,
       JaxbVideosXml.class,
       JaxbVideosXml.class,
       new Annotation[0],
       MediaType.APPLICATION_XML_TYPE,
       new MetadataMap<String, Object>(),
       baos);
   return new XmlPath(baos.toString());
 }
  protected void run() {
    Bus bus = BusFactory.getDefaultBus();
    setBus(bus);
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setBus(bus);
    sf.setResourceClasses(BookStore.class, SimpleBookStore.class, BookStorePerRequest.class);

    List<Object> providers = new ArrayList<Object>();

    // default lifecycle is per-request, change it to singleton
    BinaryDataProvider<Object> p = new BinaryDataProvider<Object>();
    p.setProduceMediaTypes(Collections.singletonList("application/bar"));
    p.setEnableBuffering(true);
    providers.add(p);
    JAXBElementProvider<?> jaxbProvider = new JAXBElementProvider<Object>();
    Map<String, String> jaxbElementClassMap = new HashMap<String, String>();
    jaxbElementClassMap.put(BookNoXmlRootElement.class.getName(), "BookNoXmlRootElement");
    jaxbProvider.setJaxbElementClassMap(jaxbElementClassMap);
    providers.add(jaxbProvider);
    providers.add(new FormatResponseHandler());
    providers.add(new GenericHandlerWriter());
    providers.add(new FaultyRequestHandler());
    providers.add(new SearchContextProvider());
    sf.setProviders(providers);
    List<Interceptor<? extends Message>> outInts = new ArrayList<Interceptor<? extends Message>>();
    outInts.add(new CustomOutInterceptor());
    sf.setOutInterceptors(outInts);
    List<Interceptor<? extends Message>> outFaultInts =
        new ArrayList<Interceptor<? extends Message>>();
    outFaultInts.add(new CustomOutFaultInterceptor());
    sf.setOutFaultInterceptors(outFaultInts);
    sf.setResourceProvider(BookStore.class, new SingletonResourceProvider(new BookStore(), true));
    sf.setAddress("http://localhost:" + PORT + "/");

    sf.getProperties(true).put("org.apache.cxf.jaxrs.mediaTypeCheck.strict", true);

    server = sf.create();
    BusFactory.setDefaultBus(null);
    BusFactory.setThreadDefaultBus(null);
  }