use of com.thoughtworks.xstream.io.xml.XppReader in project intellij-community by JetBrains.
the class ProtocolParser method parseThread.
@NotNull
public static PyThreadInfo parseThread(final String text, final PyPositionConverter positionConverter) throws PyDebuggerException {
final XppReader reader = openReader(text, true);
reader.moveDown();
if (!"thread".equals(reader.getNodeName())) {
throw new PyDebuggerException("Expected <thread>, found " + reader.getNodeName());
}
final String id = readString(reader, "id", null);
final String name = readString(reader, "name", "");
final int stopReason = readInt(reader, "stop_reason", 0);
String message = readString(reader, "message", "None");
if ("None".equals(message) || message.isEmpty()) {
message = null;
}
final List<PyStackFrameInfo> frames = new LinkedList<>();
while (reader.hasMoreChildren()) {
reader.moveDown();
frames.add(parseFrame(reader, id, positionConverter));
reader.moveUp();
}
return new PyThreadInfo(id, name, frames, stopReason, message);
}
use of com.thoughtworks.xstream.io.xml.XppReader in project intellij-community by JetBrains.
the class ProtocolParser method parseConcurrencyEvent.
public static PyConcurrencyEvent parseConcurrencyEvent(String payload, final PyPositionConverter positionConverter) throws PyDebuggerException {
final XppReader reader = openReader(payload, true);
reader.moveDown();
String eventName = reader.getNodeName();
boolean isAsyncio;
if (eventName.equals("threading_event")) {
isAsyncio = false;
} else if (eventName.equals("asyncio_event")) {
isAsyncio = true;
} else {
throw new PyDebuggerException("Expected <threading_event> or <asyncio_event>, found " + reader.getNodeName());
}
final Long time = Long.parseLong(readString(reader, "time", ""));
final String name = readString(reader, "name", "");
final String thread_id = readString(reader, "thread_id", "");
final String type = readString(reader, "type", "");
PyConcurrencyEvent threadingEvent;
if (type.equals("lock")) {
String lock_id = readString(reader, "lock_id", "0");
threadingEvent = new PyLockEvent(time, thread_id, name, lock_id, isAsyncio);
} else if (type.equals("thread")) {
String parentThread = readString(reader, "parent", "");
if (!parentThread.isEmpty()) {
threadingEvent = new PyThreadEvent(time, thread_id, name, parentThread, isAsyncio);
} else {
threadingEvent = new PyThreadEvent(time, thread_id, name, isAsyncio);
}
} else {
throw new PyDebuggerException("Unknown type " + type);
}
final String eventType = readString(reader, "event", "");
if (eventType.equals("__init__")) {
threadingEvent.setType(PyConcurrencyEvent.EventType.CREATE);
} else if (eventType.equals("start")) {
threadingEvent.setType(PyConcurrencyEvent.EventType.START);
} else if (eventType.equals("join")) {
threadingEvent.setType(PyConcurrencyEvent.EventType.JOIN);
} else if (eventType.equals("stop")) {
threadingEvent.setType(PyConcurrencyEvent.EventType.STOP);
} else if (eventType.equals("acquire_begin") || eventType.equals("__enter___begin") || (eventType.equals("get_begin")) || (eventType.equals("put_begin"))) {
threadingEvent.setType(PyConcurrencyEvent.EventType.ACQUIRE_BEGIN);
} else if (eventType.equals("acquire_end") || eventType.equals("__enter___end") || (eventType.equals("get_end")) || (eventType.equals("put_end"))) {
threadingEvent.setType(PyConcurrencyEvent.EventType.ACQUIRE_END);
} else if (eventType.startsWith("release") || eventType.startsWith("__exit__")) {
// we record release begin and end on the Python side, but it is not important info
// for user. Maybe use it later
threadingEvent.setType(PyConcurrencyEvent.EventType.RELEASE);
} else {
throw new PyDebuggerException("Unknown event " + eventType);
}
threadingEvent.setFileName(readString(reader, "file", ""));
threadingEvent.setLine(Integer.parseInt(readString(reader, "line", "")) - 1);
reader.moveUp();
final List<PyStackFrameInfo> frames = new LinkedList<>();
while (reader.hasMoreChildren()) {
reader.moveDown();
frames.add(parseFrame(reader, thread_id, positionConverter));
reader.moveUp();
}
threadingEvent.setFrames(frames);
return threadingEvent;
}
use of com.thoughtworks.xstream.io.xml.XppReader in project ddf by codice.
the class TestCswRecordConverter method testUnmarshalWriteNamespaces.
@Test
public void testUnmarshalWriteNamespaces() throws IOException, SAXException, XmlPullParserException {
XStream xstream = new XStream(new XppDriver());
xstream.registerConverter(converter);
xstream.alias("Record", MetacardImpl.class);
xstream.alias("csw:Record", MetacardImpl.class);
InputStream is = IOUtils.toInputStream(getRecordNoNamespaceDeclaration());
HierarchicalStreamReader reader = new XppReader(new InputStreamReader(is), XmlPullParserFactory.newInstance().newPullParser());
DataHolder args = xstream.newDataHolder();
Map<String, String> namespaces = new HashMap<>();
namespaces.put(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.CSW_NAMESPACE_PREFIX, CswConstants.CSW_OUTPUT_SCHEMA);
namespaces.put(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.DUBLIN_CORE_NAMESPACE_PREFIX, CswConstants.DUBLIN_CORE_SCHEMA);
namespaces.put(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.DUBLIN_CORE_TERMS_NAMESPACE_PREFIX, CswConstants.DUBLIN_CORE_TERMS_SCHEMA);
namespaces.put(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.OWS_NAMESPACE_PREFIX, CswConstants.OWS_NAMESPACE);
args.put(CswConstants.NAMESPACE_DECLARATIONS, namespaces);
Metacard mc = (Metacard) xstream.unmarshal(reader, null, args);
Metacard expectedMetacard = getTestMetacard();
assertThat(mc, notNullValue());
assertThat(mc.getContentTypeName(), is(mc.getContentTypeName()));
assertThat(mc.getCreatedDate(), is(expectedMetacard.getCreatedDate()));
assertThat(mc.getEffectiveDate(), is(expectedMetacard.getEffectiveDate()));
assertThat(mc.getId(), is(expectedMetacard.getId()));
assertThat(mc.getModifiedDate(), is(expectedMetacard.getModifiedDate()));
assertThat(mc.getTitle(), is(expectedMetacard.getTitle()));
assertThat(mc.getResourceURI(), is(expectedMetacard.getResourceURI()));
XMLUnit.setIgnoreWhitespace(true);
assertXMLEqual(mc.getMetadata(), getControlRecord());
}
use of com.thoughtworks.xstream.io.xml.XppReader in project ddf by codice.
the class GetRecordsMessageBodyReader method readFrom.
@Override
public CswRecordCollection readFrom(Class<CswRecordCollection> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream inStream) throws IOException, WebApplicationException {
CswRecordCollection cswRecords = null;
Map<String, Serializable> resourceProperties = new HashMap<>();
// Check if the server returned a Partial Content response (hopefully in response to a range header)
String contentRangeHeader = httpHeaders.getFirst(HttpHeaders.CONTENT_RANGE);
if (StringUtils.isNotBlank(contentRangeHeader)) {
contentRangeHeader = StringUtils.substringBetween(contentRangeHeader.toLowerCase(), "bytes ", "-");
long bytesSkipped = Long.parseLong(contentRangeHeader);
resourceProperties.put(BYTES_SKIPPED, Long.valueOf(bytesSkipped));
}
// If the following HTTP header exists and its value is true, the input stream will contain
// raw product data
String productRetrievalHeader = httpHeaders.getFirst(CswConstants.PRODUCT_RETRIEVAL_HTTP_HEADER);
if (productRetrievalHeader != null && productRetrievalHeader.equalsIgnoreCase("TRUE")) {
String fileName = handleContentDispositionHeader(httpHeaders);
cswRecords = new CswRecordCollection();
cswRecords.setResource(new ResourceImpl(inStream, mediaType.toString(), fileName));
cswRecords.setResourceProperties(resourceProperties);
return cswRecords;
}
// Save original input stream for any exception message that might need to be
// created
String originalInputStream = IOUtils.toString(inStream, "UTF-8");
LOGGER.debug("Converting to CswRecordCollection: \n {}", originalInputStream);
// Re-create the input stream (since it has already been read for potential
// exception message creation)
inStream = new ByteArrayInputStream(originalInputStream.getBytes("UTF-8"));
try {
HierarchicalStreamReader reader = new XppReader(new InputStreamReader(inStream, StandardCharsets.UTF_8), XmlPullParserFactory.newInstance().newPullParser());
cswRecords = (CswRecordCollection) xstream.unmarshal(reader, null, argumentHolder);
} catch (XmlPullParserException e) {
LOGGER.debug("Unable to create XmlPullParser, and cannot parse CSW Response.", e);
} catch (XStreamException e) {
// If an ExceptionReport is sent from the remote CSW site it will be sent with an
// JAX-RS "OK" status, hence the ErrorResponse exception mapper will not fire.
// Instead the ExceptionReport will come here and be treated like a GetRecords
// response, resulting in an XStreamException since ExceptionReport cannot be
// unmarshalled. So this catch clause is responsible for catching that XStream
// exception and creating a JAX-RS response containing the original stream
// (with the ExceptionReport) and rethrowing it as a WebApplicatioNException,
// which CXF will wrap as a ClientException that the CswSource catches, converts
// to a CswException, and logs.
ByteArrayInputStream bis = new ByteArrayInputStream(originalInputStream.getBytes(StandardCharsets.UTF_8));
ResponseBuilder responseBuilder = Response.ok(bis);
responseBuilder.type("text/xml");
Response response = responseBuilder.build();
throw new WebApplicationException(e, response);
} finally {
IOUtils.closeQuietly(inStream);
}
return cswRecords;
}
use of com.thoughtworks.xstream.io.xml.XppReader in project ddf by codice.
the class TestCswTransformProvider method testUnmarshalMissingNamespaces.
@Test
public void testUnmarshalMissingNamespaces() throws Exception {
InputTransformer mockInputTransformer = mock(InputTransformer.class);
when(mockInputManager.getTransformerBySchema(anyString())).thenReturn(mockInputTransformer);
Map<String, String> namespaces = new HashMap<>();
namespaces.put("xmlns:csw", "http://www.opengis.net/cat/csw/2.0.2");
namespaces.put("xmlns:dc", "http://purl.org/dc/elements/1.1/");
namespaces.put("xmlns:dct", "http://purl.org/dc/terms/");
HierarchicalStreamReader reader = new XppReader(new StringReader(getRecordMissingNamespaces()), XmlPullParserFactory.newInstance().newPullParser());
CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
context.put(CswConstants.NAMESPACE_DECLARATIONS, namespaces);
context.put(CswConstants.OUTPUT_SCHEMA_PARAMETER, OTHER_SCHEMA);
ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
provider.unmarshal(reader, context);
// Verify the context arguments were set correctly
verify(mockInputTransformer, times(1)).transform(captor.capture());
InputStream inStream = captor.getValue();
String result = IOUtils.toString(inStream);
XMLUnit.setIgnoreWhitespace(true);
XMLAssert.assertXMLEqual(getRecord(), result);
}
Aggregations