Skip to content

Commit

Permalink
Deleted parts from JaxbDomHack
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Oct 5, 2016
1 parent 02fb711 commit cab6e15
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 214 deletions.
Expand Up @@ -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<Package> 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 <T extends Containerable> ItemDefinition locateItemDefinition(
PrismContainerDefinition<T> containerDefinition, QName elementQName, Object valueElements)
throws SchemaException {
Expand Down Expand Up @@ -237,24 +213,6 @@ public <IV extends PrismValue,ID extends ItemDefinition,C extends Containerable>
return subItem;
}

// public <V extends PrismValue, C extends Containerable> Collection<Item<V>> fromAny(List<Object> anyObjects, PrismContainerDefinition<C> definition) throws SchemaException {
// Collection<Item<V>> items = new ArrayList<>();
// for (Object anyObject: anyObjects) {
// Item<V> newItem = parseRawElement(anyObject, definition);
// boolean merged = false;
// for (Item<V> 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.
Expand Down Expand Up @@ -312,162 +270,6 @@ public Object toAny(PrismValue value) throws SchemaException {
return xmlValue;
}

// public <O extends Objectable> PrismObject<O> 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<O>)objectElement).getValue();
// prismContext.adopt(jaxbValue);
// return jaxbValue.asPrismObject();
// } else {
// throw new IllegalArgumentException("Unknown element type "+objectElement.getClass());
// }
// }

// public <O extends Objectable> Element serializeObjectToJaxb(PrismObject<O> object) throws SchemaException {
// RootXNode xroot = prismContext.getXnodeProcessor().serializeObject(object);
// return domParser.serializeXRootToElement(xroot);
// }

// public <T> Element marshalJaxbObjectToDom(T jaxbObject, QName elementQName) throws JAXBException {
// return marshalJaxbObjectToDom(jaxbObject, elementQName, (Document) null);
// }

// public <T> Element marshalJaxbObjectToDom(T jaxbObject, QName elementQName, Document doc) throws JAXBException {
// if (doc == null) {
// doc = DOMUtil.getDocument();
// }
//
// JAXBElement<T> jaxbElement = new JAXBElement<T>(elementQName, (Class<T>) 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<String, Object> 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<String, Object> property : jaxbProperties.entrySet()) {
// marshaller.setProperty(property.getKey(), property.getValue());
// }
// }
//
// return marshaller;
// }

// public <T> T toJavaValue(Element element, Class<T> 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> T unmarshalObject(InputStream input) throws JAXBException, SchemaException {
// Object object = createUnmarshaller().unmarshal(input);
// JAXBElement<T> jaxbElement = (JAXBElement<T>) 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<String, Object>());
// }

// public String marshalElementToString(JAXBElement<?> jaxbElement, Map<String, Object> properties) throws JAXBException {
// StringWriter writer = new StringWriter();
// Marshaller marshaller = createMarshaller(null);
// for (Entry<String, Object> 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);
// }

}
Expand Up @@ -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;
Expand Down Expand Up @@ -73,21 +74,23 @@ public void writeTo(T object, Class<?> type, Type genericType,
MultivaluedMap<String, Object> 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
Expand Down

0 comments on commit cab6e15

Please sign in to comment.