diff --git a/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/JaxbDomHack.java b/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/JaxbDomHack.java index 0ddddfa1ca1..583fcb3e9bf 100644 --- a/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/JaxbDomHack.java +++ b/infra/prism/src/main/java/com/evolveum/midpoint/prism/marshaller/JaxbDomHack.java @@ -54,37 +54,13 @@ public class JaxbDomHack { private PrismContext prismContext; private DomParser domParser; -// private JAXBContext jaxbContext; public JaxbDomHack(DomParser domParser, PrismContext prismContext) { super(); this.domParser = domParser; this.prismContext = prismContext; -// initializeJaxbContext(); } -// private void initializeJaxbContext() { -// StringBuilder sb = new StringBuilder(); -// Iterator iterator = prismContext.getSchemaRegistry().getCompileTimePackages().iterator(); -// while (iterator.hasNext()) { -// Package jaxbPackage = iterator.next(); -// sb.append(jaxbPackage.getName()); -// if (iterator.hasNext()) { -// sb.append(":"); -// } -// } -// String jaxbPaths = sb.toString(); -// if (jaxbPaths.isEmpty()) { -// LOGGER.debug("No JAXB paths, skipping creation of JAXB context"); -// } else { -// try { -// jaxbContext = JAXBContext.newInstance(jaxbPaths); -// } catch (JAXBException ex) { -// throw new SystemException("Couldn't create JAXBContext for: " + jaxbPaths, ex); -// } -// } -// } - private ItemDefinition locateItemDefinition( PrismContainerDefinition containerDefinition, QName elementQName, Object valueElements) throws SchemaException { @@ -237,24 +213,6 @@ public return subItem; } -// public Collection> fromAny(List anyObjects, PrismContainerDefinition definition) throws SchemaException { -// Collection> items = new ArrayList<>(); -// for (Object anyObject: anyObjects) { -// Item newItem = parseRawElement(anyObject, definition); -// boolean merged = false; -// for (Item existingItem: items) { -// if (newItem.getElementName().equals(existingItem.getElementName())) { -// existingItem.merge(newItem); -// merged = true; -// break; -// } -// } -// if (!merged) { -// items.add(newItem); -// } -// } -// return items; -// } /** * Serializes prism value to JAXB "any" format as returned by JAXB getAny() methods. @@ -312,162 +270,6 @@ public Object toAny(PrismValue value) throws SchemaException { return xmlValue; } -// public PrismObject parseObjectFromJaxb(Object objectElement) throws SchemaException { -// if (objectElement instanceof Element) { -// // DOM -// XNode objectXNode = domParser.parseElementContent((Element)objectElement); -// return prismContext.getXnodeProcessor().parseObject(objectXNode); -// } else if (objectElement instanceof JAXBElement) { -// O jaxbValue = ((JAXBElement)objectElement).getValue(); -// prismContext.adopt(jaxbValue); -// return jaxbValue.asPrismObject(); -// } else { -// throw new IllegalArgumentException("Unknown element type "+objectElement.getClass()); -// } -// } - -// public Element serializeObjectToJaxb(PrismObject object) throws SchemaException { -// RootXNode xroot = prismContext.getXnodeProcessor().serializeObject(object); -// return domParser.serializeXRootToElement(xroot); -// } - -// public Element marshalJaxbObjectToDom(T jaxbObject, QName elementQName) throws JAXBException { -// return marshalJaxbObjectToDom(jaxbObject, elementQName, (Document) null); -// } - -// public Element marshalJaxbObjectToDom(T jaxbObject, QName elementQName, Document doc) throws JAXBException { -// if (doc == null) { -// doc = DOMUtil.getDocument(); -// } -// -// JAXBElement jaxbElement = new JAXBElement(elementQName, (Class) jaxbObject.getClass(), -// jaxbObject); -// Element element = doc.createElementNS(elementQName.getNamespaceURI(), elementQName.getLocalPart()); -// marshalElementToDom(jaxbElement, element); -// -// return (Element) element.getFirstChild(); -// } - -// private void marshalElementToDom(JAXBElement jaxbElement, Node parentNode) throws JAXBException { -// createMarshaller(null).marshal(jaxbElement, parentNode); -// } - -// private Marshaller createMarshaller(Map jaxbProperties) throws JAXBException { -// Marshaller marshaller = jaxbContext.createMarshaller(); -// // set default properties -// marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); -// marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); -// DynamicNamespacePrefixMapper namespacePrefixMapper = prismContext.getSchemaRegistry().getNamespacePrefixMapper().clone(); -// namespacePrefixMapper.setAlwaysExplicit(true); -// marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", namespacePrefixMapper); -// // set custom properties -// if (jaxbProperties != null) { -// for (Entry property : jaxbProperties.entrySet()) { -// marshaller.setProperty(property.getKey(), property.getValue()); -// } -// } -// -// return marshaller; -// } - -// public T toJavaValue(Element element, Class typeClass) throws JAXBException { -// QName type = JAXBUtil.getTypeQName(typeClass); -// return (T) toJavaValue(element, type); -// } - - /** - * Used to convert property values from DOM - */ -// private Object toJavaValue(Element element, QName xsdType) throws JAXBException { -// Class declaredType = prismContext.getSchemaRegistry().getCompileTimeClass(xsdType); -// if (declaredType == null) { -// // This may happen if the schema is runtime and there is no associated compile-time class -// throw new SystemException("Cannot determine Java type for "+xsdType); -// } -// JAXBElement jaxbElement = createUnmarshaller().unmarshal(element, declaredType); -// Object object = jaxbElement.getValue(); -// return object; -// } - -// private Unmarshaller createUnmarshaller() throws JAXBException { -// return jaxbContext.createUnmarshaller(); -// } - -// public JAXBElement unmarshalJaxbElement(File input) throws JAXBException, IOException { -// return (JAXBElement) createUnmarshaller().unmarshal(input); -// } - -// public T unmarshalObject(InputStream input) throws JAXBException, SchemaException { -// Object object = createUnmarshaller().unmarshal(input); -// JAXBElement jaxbElement = (JAXBElement) object; -// adopt(jaxbElement); -// -// if (jaxbElement == null) { -// return null; -// } else { -// return jaxbElement.getValue(); -// } -// } - -// private void adopt(Object object) throws SchemaException { -// if (object instanceof JAXBElement) { -// adopt(((JAXBElement)object).getValue()); -// } else if (object instanceof Objectable) { -// prismContext.adopt(((Objectable) (object))); -// } -// } - - public String silentMarshalObject(Object object, Trace logger) { - String xml = null; - try { - QName fakeQName=new QName(PrismConstants.NS_PREFIX + "debug", "debugPrintObject"); - if (object instanceof Objectable) { - xml = prismContext.serializeObjectToString(((Objectable) object).asPrismObject(), PrismContext.LANG_XML); - } else if (object instanceof Containerable) { - xml = prismContext.xmlSerializer().serialize(((Containerable) object).asPrismContainerValue(), fakeQName); - } else { - xml = prismContext.xmlSerializer().serializeAnyData(object, fakeQName); - } - } catch (Exception ex) { - Trace log = logger != null ? logger : LOGGER; - LoggingUtils.logException(log, "Couldn't marshal element to string {}", ex, object); - } - return xml; - } - -// public String marshalElementToString(JAXBElement jaxbElement) throws JAXBException { -// return marshalElementToString(jaxbElement, new HashMap()); -// } - -// public String marshalElementToString(JAXBElement jaxbElement, Map properties) throws JAXBException { -// StringWriter writer = new StringWriter(); -// Marshaller marshaller = createMarshaller(null); -// for (Entry entry : properties.entrySet()) { -// marshaller.setProperty(entry.getKey(), entry.getValue()); -// } -// marshaller.marshal(jaxbElement, writer); -// return writer.getBuffer().toString(); -// } - -// public boolean isJaxbClass(Class clazz) { -// if (clazz == null) { -// throw new IllegalArgumentException("No class, no fun"); -// } -// if (clazz.getPackage() == null) { -// // No package: this is most likely a primitive type and definitely -// // not a JAXB class -// return false; -// } -// for (Package jaxbPackage: prismContext.getSchemaRegistry().getCompileTimePackages()) { -// if (jaxbPackage.equals(clazz.getPackage())) { -// return true; -// } -// } -// return false; -// } -// public boolean canConvert(Class clazz) { -// return isJaxbClass(clazz); -// } } diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/rest/MidpointXmlProvider.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/rest/MidpointXmlProvider.java index 2195275cc05..d9c7886ec3c 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/rest/MidpointXmlProvider.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/rest/MidpointXmlProvider.java @@ -17,14 +17,15 @@ import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; import javax.xml.bind.JAXBException; +import javax.xml.namespace.QName; +import com.evolveum.midpoint.prism.*; +import com.evolveum.midpoint.util.logging.LoggingUtils; import org.apache.cxf.jaxrs.model.ClassResourceInfo; import org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider; import org.apache.cxf.jaxrs.provider.JAXBElementProvider; import org.springframework.beans.factory.annotation.Autowired; -import com.evolveum.midpoint.prism.PrismContext; -import com.evolveum.midpoint.prism.PrismObject; import com.evolveum.midpoint.prism.delta.ItemDelta; import com.evolveum.midpoint.schema.DeltaConvertor; import com.evolveum.midpoint.schema.result.OperationResult; @@ -73,21 +74,23 @@ public void writeTo(T object, Class type, Type genericType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { - - String marhaledObj = null; - - if (object instanceof PrismObject){ - marhaledObj = prismContext.getJaxbDomHack().silentMarshalObject(((PrismObject) object).asObjectable(), LOGGER); - } else if (object instanceof OperationResult){ - OperationResultType operationResultType = ((OperationResult) object).createOperationResultType(); - marhaledObj = prismContext.getJaxbDomHack().silentMarshalObject(operationResultType, LOGGER); - } else{ - marhaledObj = prismContext.getJaxbDomHack().silentMarshalObject(object, LOGGER); + + // TODO implement in the standard serializer; also change root name + QName fakeQName = new QName(PrismConstants.NS_PREFIX + "debug", "debugPrintObject"); + String xml; + try { + if (object instanceof PrismObject) { + xml = prismContext.xmlSerializer().serialize((PrismObject) object); + } else if (object instanceof OperationResult) { + OperationResultType operationResultType = ((OperationResult) object).createOperationResultType(); + xml = prismContext.xmlSerializer().serializeAnyData(operationResultType, fakeQName); + } else { + xml = prismContext.xmlSerializer().serializeAnyData(object, fakeQName); + } + entityStream.write(xml.getBytes("utf-8")); + } catch (SchemaException | RuntimeException e) { + LoggingUtils.logException(LOGGER, "Couldn't marshal element to string: {}", e, object); } - - entityStream.write(marhaledObj.getBytes("utf-8")); - - } @Override