public void testCreateContextUnrelatedSessionsXmlInvalidPath() throws Exception {
   try {
     JAXBContext context =
         JAXBContextFactory.createContext(
             "org.eclipse.persistence.testing.jaxb.jaxbcontext.fake",
             new ClassLoader() {
               public URL getResource(String resourceName) {
                 if (resourceName.equals("sessions.xml")) {
                   return getParent()
                       .getResource(
                           "org/eclipse/persistence/testing/jaxb/jaxbcontext/sessions.xml");
                 }
                 return this.getParent().getResource(resourceName);
               }
             });
   } catch (JAXBException ex) {
     assertTrue(
         ((org.eclipse.persistence.exceptions.JAXBException) ex.getLinkedException())
                 .getErrorCode()
             == org.eclipse.persistence.exceptions.JAXBException
                 .NO_OBJECT_FACTORY_OR_JAXB_INDEX_IN_PATH);
     assertTrue(
         ((org.eclipse.persistence.exceptions.JAXBException) ex.getLinkedException())
                 .getInternalException()
             instanceof SessionLoaderException);
   }
 }
  @Override
  public String prepareResponse(String className, Object o) throws ParseException {
    try {
      Class clazz = Class.forName(className);

      JAXBContext jaxbContext = JAXBContext.newInstance(clazz.getPackage().getName());
      Marshaller marshaller = jaxbContext.createMarshaller();
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      marshaller.marshal(clazz.cast(o), byteArrayOutputStream);

      return byteArrayOutputStream.toString();

    } catch (JAXBException e) {
      throw new ParseException(
          "Error occured during the parsing of request's XML body. The details of the exception "
              + e.toString(),
          0);
    } catch (ClassNotFoundException e) {
      throw new ParseException(
          "Error occured during the parsing of request's XML body. The details of the exception "
              + e.toString(),
          0);
    }
  }
示例#3
0
    Context(Class clazz) {
      try {
        jaxbContext = JAXBContext.newInstance(clazz);

        marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setSchema(null);

        unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setSchema(null);
      } catch (JAXBException e) {
        e.printStackTrace();
      }
    }
 public void testCreateConcreteClassWithMultiArgConstructor() throws JAXBException {
   try {
     Class[] classes = new Class[1];
     classes[0] = ConcreteClassWithMultiArgConstructor.class;
     JAXBContextFactory.createContext(classes, null);
   } catch (JAXBException e) {
     org.eclipse.persistence.exceptions.JAXBException je =
         (org.eclipse.persistence.exceptions.JAXBException) e.getLinkedException();
     assertEquals(
         org.eclipse.persistence.exceptions.JAXBException.FACTORY_METHOD_OR_ZERO_ARG_CONST_REQ,
         je.getErrorCode());
     return;
   }
   fail();
 }
示例#5
0
 /** Used to retrieve the root document bound through JAXB */
 private static void getRootDocument() {
   if (rDocument == null) {
     try {
       JAXBContext jaxbCtxt = JAXBContext.newInstance("odin.odin.xml");
       Unmarshaller jaxbU = jaxbCtxt.createUnmarshaller();
       jaxbU.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
       File fRoot = new File("home.root.xml");
       JAXBElement<Root> rElement = jaxbU.unmarshal(new StreamSource(fRoot), Root.class);
       rDocument = rElement.getValue();
     } catch (JAXBException jaxbX) {
       System.err.println(
           "An error was caught while trying to read information from the server's root information document.");
       jaxbX.printStackTrace();
     }
   }
 }
 public void testCreateContextNoClassesOrSessions() throws Exception {
   try {
     JAXBContext context =
         JAXBContextFactory.createContext(
             "org.eclipse.persistence.testing.jaxb.jaxbcontext.fake",
             Thread.currentThread().getContextClassLoader());
   } catch (JAXBException ex) {
     assertTrue(
         ((org.eclipse.persistence.exceptions.JAXBException) ex.getLinkedException())
                 .getErrorCode()
             == org.eclipse.persistence.exceptions.JAXBException
                 .NO_OBJECT_FACTORY_OR_JAXB_INDEX_IN_PATH);
     assertTrue(
         ((org.eclipse.persistence.exceptions.JAXBException) ex.getLinkedException())
                 .getInternalException()
             instanceof ValidationException);
   }
 }
  @Override
  public Object parseRequest(String className) throws ParseException {
    try {
      Class clazz = Class.forName(className);
      JAXBContext jaxbContext = JAXBContext.newInstance(clazz.getPackage().getName());
      Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

      Source source = new StreamSource(new ByteArrayInputStream(body.getBytes()));
      JAXBElement<Object> root = jaxbUnmarshaller.unmarshal(source, clazz);

      return clazz.cast(root.getValue());

    } catch (JAXBException e) {
      throw new ParseException(
          "Error occured during the parsing of request's XML body. The details of the exception "
              + e.toString(),
          0);
    } catch (ClassNotFoundException e) {
      throw new ParseException(
          "Error occured during the parsing of request's XML body. The details of the exception "
              + e.toString(),
          0);
    }
  }