org.eclipse.persistence.jaxb.UnmarshallerProperties Java Examples

The following examples show how to use org.eclipse.persistence.jaxb.UnmarshallerProperties. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JaxbUtils.java    From TranskribusCore with GNU General Public License v3.0 6 votes vote down vote up
/**
	 * Experimental method for loading an object from a JSON String.
	 * Seems to work with {@link #marshalToJsonStringWithJaxb(Object, boolean)}
	 * 
	 * @param str
	 * @param targetClass
	 * @param nestedClasses
	 * @return
	 * @throws JAXBException
	 */
	public static <T> T unmarshalJson(String str, Class<T> targetClass, Class<?>... nestedClasses) throws JAXBException {
		//this part only works for the representations produced via the JAXB API
		Class<?>[] targetClasses = merge(targetClass, nestedClasses);
		JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(targetClasses, Collections.<String,Object>emptyMap());
		Unmarshaller u = jc.createUnmarshaller();
		u.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
		u.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
		
		StringReader sr = new StringReader(str);
//		@SuppressWarnings("unchecked")
//		T object = (T) u.unmarshal(sr);
//		return object;
		
		StreamSource source = new StreamSource(sr);
		JAXBElement<T> element = u.unmarshal(source, targetClass);

		return element.getValue();
	}
 
Example #2
Source File: JSONConvertor.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void initialize() throws JAXBException {
	um = context.createUnmarshaller();
	// Set the Unmarshaller media type to JSON or XML
	um.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
	// Set it to true if you need to include the JSON root element in the
	//um.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, IsJsonIncludeRoot);
	

	m = context.createMarshaller();		
	//m.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
	// Set it to true if you need to include the JSON root element in the JSON output
	//m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
	// Set it to true if you need the JSON output to formatted
	m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
	
}
 
Example #3
Source File: DaoJSONConvertor.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void initialize() throws JAXBException {
		um = context.createUnmarshaller();
		// Set the Unmarshaller media type to JSON or XML
		um.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
		// Set it to true if you need to include the JSON root element in the
//		um.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, isJsonIncludeRoot);
		

		m = context.createMarshaller();		
		//m.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
		// Set it to true if you need to include the JSON root element in the JSON output
		//m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
		// Set it to true if you need the JSON output to formatted
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		
	}
 
Example #4
Source File: XMLConvertor.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void initialize(String schema) throws JAXBException {

		um = context.createUnmarshaller();
		um.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/xml");
	
		m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper",
				new NamespacePrefixMapper() {
					@Override
					public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
						if(namespaceUri.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE)) {
							return "xsi";
						} else 
							return "m2m";
					}
				});
		String schemaLocation = schema;

		if(schemaLocation != null) {
			m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.onem2m.org/xml/protocols " + schemaLocation);
		}
		m.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
	}
 
Example #5
Source File: JSONConvertor.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void initialize() throws JAXBException {
	um = context.createUnmarshaller();
	// Set the Unmarshaller media type to JSON or XML
	um.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
	// Set it to true if you need to include the JSON root element in the
	//um.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, IsJsonIncludeRoot);
	

	m = context.createMarshaller();		
	//m.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
	// Set it to true if you need to include the JSON root element in the JSON output
	//m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
	// Set it to true if you need the JSON output to formatted
	m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
	
}
 
Example #6
Source File: DaoJSONConvertor.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void initialize() throws JAXBException {
		um = context.createUnmarshaller();
		// Set the Unmarshaller media type to JSON or XML
		um.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
		// Set it to true if you need to include the JSON root element in the
//		um.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, isJsonIncludeRoot);
		

		m = context.createMarshaller();		
		//m.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
		// Set it to true if you need to include the JSON root element in the JSON output
		//m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
		// Set it to true if you need the JSON output to formatted
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		
	}
 
Example #7
Source File: XMLConvertor.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void initialize(String schema) throws JAXBException {

		um = context.createUnmarshaller();
		um.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/xml");
	
		m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper",
				new NamespacePrefixMapper() {
					@Override
					public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
						if(namespaceUri.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE)) {
							return "xsi";
						} else 
							return "m2m";
					}
				});
		String schemaLocation = schema;

		if(schemaLocation != null) {
			m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.onem2m.org/xml/protocols " + schemaLocation);
		}
		m.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
	}
 
Example #8
Source File: TopologyMarshaller.java    From knox with Apache License 2.0 5 votes vote down vote up
@Override
public Topology readFrom(Class<Topology>                type,
                         Type                           genericType,
                         Annotation[]                   annotations,
                         MediaType                      mediaType,
                         MultivaluedMap<String, String> httpHeaders,
                         InputStream                    entityStream) throws IOException, WebApplicationException {
  Topology topology = null;

  try {
    if (isReadable(type, genericType, annotations, mediaType)) {
      Map<String, Object> properties = Collections.emptyMap();
      JAXBContext context = JAXBContext.newInstance(new Class[]{Topology.class}, properties);

      Unmarshaller u = context.createUnmarshaller();
      u.setProperty(UnmarshallerProperties.MEDIA_TYPE, mediaType.getType() + "/" + mediaType.getSubtype());

      if (mediaType.isCompatible(MediaType.APPLICATION_XML_TYPE)) {
        // Safeguard against entity injection (KNOX-1308)
        XMLInputFactory xif = XMLInputFactory.newFactory();
        xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
        XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(entityStream));
        topology = (Topology) u.unmarshal(xsr);
      } else {
        topology = (Topology) u.unmarshal(entityStream);
      }
    }
  } catch (XMLStreamException | JAXBException e) {
    throw new IOException(e);
  }

  return topology;
}
 
Example #9
Source File: JqmRestApp.java    From jqm with Apache License 2.0 4 votes vote down vote up
public JqmRestApp(@Context ServletContext context)
{
    log.debug("Starting REST WS app");

    // These two properties ensure lists are properly named in JSON objects
    this.property(MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
    this.property(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);

    // Determine which of the three APIs should be loaded
    boolean loadApiSimple;
    boolean loadApiClient;
    boolean loadApiAdmin;

    try (DbConn cnx = Helpers.getDbSession())
    {
        if (context.getInitParameter("jqmnodeid") != null)
        {
            // The application is running hosted by a JQM node.
            Node n = null;

            try
            {
                n = Node.select_single(cnx, "node_select_by_id", Integer.parseInt(context.getInitParameter("jqmnodeid")));
            }
            catch (NoResultException e)
            {
                throw new RuntimeException("invalid configuration: no node of ID " + context.getInitParameter("jqmnodeid"));
            }
            loadApiSimple = !Boolean.parseBoolean(GlobalParameter.getParameter(cnx, "disableWsApiSimple", "false"));
            loadApiClient = !Boolean.parseBoolean(GlobalParameter.getParameter(cnx, "disableWsApiClient", "false"));
            loadApiAdmin = !Boolean.parseBoolean(GlobalParameter.getParameter(cnx, "disableWsApiAdmin", "false"));

            loadApiAdmin = loadApiAdmin && (n.getLoadApiAdmin() == null ? false : n.getLoadApiAdmin());
            loadApiClient = loadApiClient && (n.getLoadApiClient() == null ? false : n.getLoadApiClient());
            loadApiSimple = loadApiSimple && (n.getLoapApiSimple() == null ? true : n.getLoapApiSimple());
        }
        else
        {
            // The application is hosted by some other server (Tomcat, JBoss... but not a JQM node)

            // Never load the simple API when not running on JQM's own server. This API relies on files that are local to the JQM
            // server.
            loadApiSimple = false;
            // Always load the two others
            loadApiAdmin = true;
            loadApiClient = true;
        }
    }

    // Load the APIs
    if (loadApiAdmin)
    {
        log.debug("\tRegistering admin service");
        this.register(ServiceAdmin.class);
    }
    if (loadApiClient)
    {
        log.debug("\tRegistering client service");
        this.register(ServiceClient.class);
    }
    if (loadApiSimple)
    {
        log.debug("\tRegistering simple service");
        this.register(ServiceSimple.class);
    }

    // Load the exception mappers
    this.register(ErrorHandler.class);
    this.register(JqmExceptionMapper.class);
    this.register(JqmInternalExceptionMapper.class);

    // Logger
    this.register(ExceptionLogger.class);

    // Load the cache annotation helper
    this.register(HttpCacheImpl.class);
}