use of com.thoughtworks.xstream.converters.ConversionException in project jmeter by apache.
the class ScriptWrapperConverter method createConversionException.
private ConversionException createConversionException(Throwable e) {
final ConversionException conversionException = new ConversionException(e);
StackTraceElement[] ste = e.getStackTrace();
if (ste != null) {
for (StackTraceElement top : ste) {
String className = top.getClassName();
if (className.startsWith("org.apache.jmeter.")) {
conversionException.add("first-jmeter-class", top.toString());
break;
}
}
}
return conversionException;
}
use of com.thoughtworks.xstream.converters.ConversionException in project ddf by codice.
the class TransactionRequestConverter method parseUpdateAction.
private UpdateAction parseUpdateAction(HierarchicalStreamReader reader, UnmarshallingContext context) {
Map<String, String> xmlnsAttributeToUriMappings = getXmlnsAttributeToUriMappingsFromContext(context);
Map<String, String> prefixToUriMappings = getPrefixToUriMappingsFromXmlnsAttributes(xmlnsAttributeToUriMappings);
String typeName = StringUtils.defaultIfEmpty(reader.getAttribute(CswConstants.TYPE_NAME_PARAMETER), CswConstants.CSW_RECORD);
String handle = StringUtils.defaultIfEmpty(reader.getAttribute(CswConstants.HANDLE_PARAMETER), "");
// Move down to the content of the <Update>.
reader.moveDown();
UpdateAction updateAction;
// Do we have a list of <RecordProperty> elements or a new <csw:Record>?
if (reader.getNodeName().contains("RecordProperty")) {
Map<String, Serializable> cswRecordProperties = new HashMap<>();
while (reader.getNodeName().contains("RecordProperty")) {
String cswField;
Serializable newValue = null;
// Move down to the <Name>.
reader.moveDown();
if (reader.getNodeName().contains("Name")) {
String attribute = reader.getValue();
cswField = CswRecordConverter.getCswAttributeFromAttributeName(attribute);
} else {
throw new ConversionException("Missing Parameter Value: missing a Name in a RecordProperty.");
}
// Move back up to the <RecordProperty>.
reader.moveUp();
String attrName = DefaultCswRecordMap.getDefaultMetacardFieldForPrefixedString(cswField);
cswRecordProperties.put(attrName, null);
// Is there a <Value>?
while (reader.hasMoreChildren()) {
// Move down to the <Value>.
reader.moveDown();
if (reader.getNodeName().contains("Value")) {
newValue = getRecordPropertyValue(reader, attrName);
} else {
throw new ConversionException("Invalid Parameter Value: invalid element in a RecordProperty.");
}
Serializable currentValue = cswRecordProperties.get(attrName);
if (currentValue != null) {
if (currentValue instanceof List) {
((List) currentValue).add(newValue);
} else {
LinkedList<Serializable> list = new LinkedList<>();
list.add(currentValue);
list.add(newValue);
cswRecordProperties.put(attrName, list);
}
} else {
cswRecordProperties.put(attrName, newValue);
}
// Back to the <RecordProperty>.
reader.moveUp();
}
// Back to the <Update>, look for the next <RecordProperty>.
reader.moveUp();
if (!reader.hasMoreChildren()) {
// Constraint, which is required.
throw new ConversionException("Missing Parameter Value: missing a Constraint.");
}
// What's the next element in the <Update>?
reader.moveDown();
}
// Now there should be a <Constraint> element.
if (reader.getNodeName().contains("Constraint")) {
StringWriter writer = new StringWriter();
XStreamAttributeCopier.copyXml(reader, writer, xmlnsAttributeToUriMappings);
QueryConstraintType constraint = getElementFromXml(writer.toString(), QueryConstraintType.class);
// For any CSW attributes that map to basic metacard attributes (e.g. title,
// modified date, etc.), update the basic metacard attributes as well.
Map<String, String> cswToMetacardAttributeNames = DefaultCswRecordMap.getDefaultCswRecordMap().getCswToMetacardAttributeNames();
Map<String, Serializable> cswRecordPropertiesWithMetacardAttributes = new HashMap<>(cswRecordProperties);
for (Entry<String, Serializable> recordProperty : cswRecordProperties.entrySet()) {
String cswAttributeName = recordProperty.getKey();
// basic metacard attribute.
if (cswToMetacardAttributeNames.containsKey(cswAttributeName)) {
String metacardAttrName = cswToMetacardAttributeNames.get(cswAttributeName);
// If this basic metacard attribute hasn't already been set, set it.
if (!cswRecordPropertiesWithMetacardAttributes.containsKey(metacardAttrName)) {
Attribute metacardAttr = cswRecordConverter.getMetacardAttributeFromCswAttribute(cswAttributeName, recordProperty.getValue(), metacardAttrName);
cswRecordPropertiesWithMetacardAttributes.put(metacardAttrName, metacardAttr.getValue());
}
}
}
updateAction = new UpdateAction(cswRecordPropertiesWithMetacardAttributes, typeName, handle, constraint, prefixToUriMappings);
} else {
throw new ConversionException("Missing Parameter Value: missing a Constraint.");
}
} else {
context.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.ID);
context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, typeName);
Metacard metacard = (Metacard) context.convertAnother(null, MetacardImpl.class, delegatingTransformer);
updateAction = new UpdateAction(metacard, typeName, handle);
// Move back to the <Update>.
reader.moveUp();
}
return updateAction;
}
use of com.thoughtworks.xstream.converters.ConversionException in project ddf by codice.
the class TransactionRequestConverter method getRecordPropertyValue.
private Serializable getRecordPropertyValue(HierarchicalStreamReader reader, String cswField) {
try {
Serializable newValue;
if (reader.hasMoreChildren()) {
reader.moveDown();
newValue = readPropertyValue(reader, cswField);
reader.moveUp();
} else {
newValue = readPropertyValue(reader, cswField);
}
return newValue;
} catch (NumberFormatException e) {
throw new ConversionException("Invalid Parameter Value: a RecordProperty " + "specified a Value that does not match the type " + cswField + " expected by for the field " + cswField, e);
}
}
use of com.thoughtworks.xstream.converters.ConversionException in project ddf by codice.
the class CswTransformProvider method unmarshal.
/**
* Creates a Metacard from the given XML. This method is not typically be called directly, instead it is
* called by another XStream Converter using UnmarshallingContext.convertAnother();
*
* @param reader
* @param context
* @return
*/
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
String keyArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_KEY);
String valArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_VALUE);
InputTransformer transformer;
if (StringUtils.isNotBlank(keyArg) && StringUtils.isNotBlank(valArg)) {
transformer = inputTransformerManager.getTransformerByProperty(keyArg, valArg);
} else {
transformer = inputTransformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
}
if (transformer == null) {
throw new ConversionException(String.format("Unable to locate a transformer for %s = %s", keyArg, valArg));
}
/* The CswRecordConverter uses unmarshal, which requires the context */
if (transformer instanceof CswRecordConverter) {
return ((CswRecordConverter) transformer).unmarshal(reader, context);
}
Metacard metacard;
try (InputStream is = readXml(reader, context)) {
InputStream inputStream = is;
if (LOGGER.isDebugEnabled()) {
String originalInputStream = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
LOGGER.debug("About to transform\n{}", originalInputStream);
inputStream = new ByteArrayInputStream(originalInputStream.getBytes(StandardCharsets.UTF_8.name()));
}
metacard = transformer.transform(inputStream);
} catch (IOException | CatalogTransformerException e) {
throw new ConversionException("Unable to transform Metacard", e);
}
return metacard;
}
use of com.thoughtworks.xstream.converters.ConversionException in project ddf by codice.
the class CswTransformProvider method marshal.
/**
* Marshals Metacards to an xml. This method is not typically be called directly, instead it is
* called by another XStream Converter using MarshallingContext.convertAnother();
*
* @param o - metacard to transform.
* @param writer - writes the XML.
* @param context - the marshalling context. Should contain a map entry for {@link
* org.codice.ddf.spatial.ogc.csw.catalog.common.CswConstants#TRANSFORMER_LOOKUP_KEY}
* {@link org.codice.ddf.spatial.ogc.csw.catalog.common.CswConstants#TRANSFORMER_LOOKUP_VALUE}
* to identify which transformer to use. Also contains properties for any
* arguments to provide the transformer.
*/
@Override
public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext context) {
if (o == null) {
return;
}
Metacard metacard = (Metacard) o;
String keyArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_KEY);
String valArg = (String) context.get(CswConstants.TRANSFORMER_LOOKUP_VALUE);
MetacardTransformer transformer;
if (StringUtils.isNotBlank(keyArg) && StringUtils.isNotBlank(valArg)) {
transformer = metacardTransformerManager.getTransformerByProperty(keyArg, valArg);
} else {
transformer = metacardTransformerManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA);
}
if (transformer == null) {
throw new ConversionException(String.format("Unable to locate a transformer for %s = %s", keyArg, valArg));
}
BinaryContent content;
try {
content = transformer.transform(metacard, getArguments(context));
} catch (CatalogTransformerException e) {
throw new ConversionException("Unable to transform Metacard", e);
}
writeXml(content, writer);
}
Aggregations