use of jolie.net.http.UnsupportedMethodException in project jolie by jolie.
the class SoapProtocol method recv_internal.
/*
* private Schema getRecvMessageValidationSchema() throws IOException { List< Source > sources = new
* ArrayList< Source >(); Definition definition = getWSDLDefinition(); if ( definition != null ) {
* Types types = definition.getTypes(); if ( types != null ) { List< ExtensibilityElement > list =
* types.getExtensibilityElements(); for( ExtensibilityElement element : list ) { if ( element
* instanceof SchemaImpl ) { sources.add( new DOMSource( ((SchemaImpl)element).getElement() ) ); } }
* } } SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI
* ); try { return schemaFactory.newSchema( sources.toArray( new Source[sources.size()] ) ); }
* catch( SAXException e ) { throw new IOException( e ); } }
*/
public CommMessage recv_internal(InputStream istream, OutputStream ostream) throws IOException {
HttpParser parser = new HttpParser(istream);
HttpMessage message = parser.parse();
String charset = HttpUtils.getCharset(null, message);
HttpUtils.recv_checkForChannelClosing(message, channel());
if (inInputPort && message.type() != HttpMessage.Type.POST) {
throw new UnsupportedMethodException("Only HTTP method POST allowed!", Method.POST);
}
encoding = message.getProperty("accept-encoding");
CommMessage retVal = null;
String messageId = "";
FaultException fault = null;
Value value = Value.create();
try {
if (message.size() > 0) {
if (checkBooleanParameter("debug")) {
interpreter.logInfo("[SOAP debug] Receiving:\n" + new String(message.content(), charset));
}
SOAPMessage soapMessage = messageFactory.createMessage();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/*
* Schema messageSchema = getRecvMessageValidationSchema(); if ( messageSchema != null ) {
* factory.setIgnoringElementContentWhitespace( true ); factory.setSchema( messageSchema ); }
*/
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource src = new InputSource(new ByteArrayInputStream(message.content()));
src.setEncoding(charset);
Document doc = builder.parse(src);
DOMSource dom = new DOMSource(doc);
soapMessage.getSOAPPart().setContent(dom);
/*
* if ( checkBooleanParameter( "debugAfter" ) ) { ByteArrayOutputStream tmpStream = new
* ByteArrayOutputStream(); soapMessage.writeTo( tmpStream ); interpreter.logInfo(
* "[SOAP debug] Receiving:\n" + tmpStream.toString() ); }
*/
SOAPFault soapFault = soapMessage.getSOAPBody().getFault();
if (soapFault == null) {
Element soapValueElement = getFirstElement(soapMessage.getSOAPBody());
messageId = soapValueElement.getLocalName();
if (!channel().parentPort().getInterface().containsOperation(messageId)) {
String[] soapAction = message.getPropertyOrEmptyString("soapaction").replaceAll("\"", "").split("/");
messageId = soapAction[soapAction.length - 1];
if (checkBooleanParameter("debug")) {
interpreter.logInfo("Operation from SoapAction:" + messageId);
}
}
// explanation: https://github.com/jolie/jolie/issues/5
xmlNodeToValue(value, soapValueElement, checkBooleanParameter("dropRootValue", false));
ValueVector schemaPaths = getParameterVector("schema");
if (schemaPaths.size() > 0) {
List<Source> sources = new LinkedList<>();
Value schemaPath;
for (int i = 0; i < schemaPaths.size(); i++) {
schemaPath = schemaPaths.get(i);
if (schemaPath.getChildren("validate").first().intValue() > 0) {
sources.add(new StreamSource(new File(schemaPaths.get(i).strValue())));
}
}
if (!sources.isEmpty()) {
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(sources.toArray(new Source[0]));
schema.newValidator().validate(new DOMSource(soapMessage.getSOAPBody().getFirstChild()));
}
}
} else {
String faultName = "UnknownFault";
Value faultValue = Value.create();
Detail d = soapFault.getDetail();
if (d != null) {
Node n = d.getFirstChild();
if (n != null) {
faultName = n.getLocalName();
xmlNodeToValue(faultValue, n, true);
} else {
faultValue.setValue(soapFault.getFaultString());
}
}
fault = new FaultException(faultName, faultValue);
}
}
String resourcePath = recv_getResourcePath(message);
if (message.isResponse()) {
if (fault != null && message.statusCode() == 500) {
fault = new FaultException("InternalServerError", "");
}
retVal = new CommMessage(CommMessage.GENERIC_ID, inputId, resourcePath, value, fault);
} else if (!message.isError()) {
if (messageId.isEmpty()) {
throw new IOException("Received SOAP Message without a specified operation");
}
retVal = new CommMessage(CommMessage.GENERIC_ID, messageId, resourcePath, value, fault);
}
final String mId = messageId;
interpreter.tracer().trace(() -> {
final StringBuilder traceMessage = new StringBuilder();
try {
traceMessage.append(getHeadersFromHttpMessage(message)).append("\n").append(new String(message.content(), charset));
return new ProtocolTraceAction(ProtocolTraceAction.Type.SOAP, "SOAP MESSAGE RECEIVED", mId, traceMessage.toString(), null);
} catch (UnsupportedEncodingException e) {
return new ProtocolTraceAction(ProtocolTraceAction.Type.SOAP, "SOAP MESSAGE RECEIVED", mId, e.getMessage(), null);
}
});
} catch (SOAPException | ParserConfigurationException e) {
throw new IOException(e);
} catch (SAXException e) {
// TODO support resourcePath
retVal = new CommMessage(CommMessage.GENERIC_ID, messageId, "/", value, new FaultException("TypeMismatch", e));
}
received = true;
if (retVal != null && "/".equals(retVal.resourcePath()) && channel().parentPort() != null && channel().parentPort().getInterface().containsOperation(retVal.operationName())) {
try {
// The message is for this service
Interface iface = channel().parentPort().getInterface();
OneWayTypeDescription oneWayTypeDescription = iface.oneWayOperations().get(retVal.operationName());
if (oneWayTypeDescription != null) {
// We are receiving a One-Way message
if (message.isResponse() == false) {
oneWayTypeDescription.requestType().cast(retVal.value());
}
} else {
RequestResponseTypeDescription rrTypeDescription = iface.requestResponseOperations().get(retVal.operationName());
if (retVal.isFault()) {
Type faultType = rrTypeDescription.faults().get(retVal.fault().faultName());
if (faultType != null) {
faultType.cast(retVal.value());
}
} else if (message.isResponse()) {
rrTypeDescription.responseType().cast(retVal.value());
} else {
rrTypeDescription.requestType().cast(retVal.value());
}
}
} catch (TypeCastingException e) {
// TODO: do something here?
}
}
return retVal;
}
use of jolie.net.http.UnsupportedMethodException in project jolie by jolie.
the class XmlRpcProtocol method recv_internal.
public CommMessage recv_internal(InputStream istream, OutputStream ostream) throws IOException {
HttpParser parser = new HttpParser(istream);
HttpMessage message = parser.parse();
String charset = HttpUtils.getCharset(null, message);
HttpUtils.recv_checkForChannelClosing(message, channel());
CommMessage retVal = null;
FaultException fault = null;
Value value = Value.create();
Document doc = null;
if (message.isError()) {
throw new IOException("HTTP error: " + new String(message.content(), charset));
}
if (inInputPort && message.type() != HttpMessage.Type.POST) {
throw new UnsupportedMethodException("Only HTTP method POST allowed!", Method.POST);
}
encoding = message.getProperty("accept-encoding");
if (message.size() > 0) {
if (getParameterVector("debug").first().intValue() > 0) {
interpreter.logInfo("[XMLRPC debug] Receiving:\n" + new String(message.content(), charset));
}
try {
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
InputSource src = new InputSource(new ByteArrayInputStream(message.content()));
src.setEncoding(charset);
doc = builder.parse(src);
if (message.isResponse()) {
// test if the message contains a fault
try {
Element faultElement = getFirstElement(doc.getDocumentElement(), "fault");
Element struct = getFirstElement(getFirstElement(faultElement, "value"), "struct");
NodeList members = struct.getChildNodes();
if (members.getLength() != 2 || members.item(1).getNodeType() != Node.ELEMENT_NODE) {
throw new IOException("Malformed fault data");
}
Element faultMember = (Element) members.item(1);
// Element valueElement = getFirstElement( getFirstElement( struct, "value" ), "string" );
String faultName = getFirstElement(faultMember, "name").getTextContent();
String faultString = getFirstElement(getFirstElement(faultMember, "value"), "string").getTextContent();
fault = new FaultException(faultName, Value.create(faultString));
} catch (IOException e) {
documentToValue(value, doc);
}
} else {
documentToValue(value, doc);
}
} catch (ParserConfigurationException | SAXException pce) {
throw new IOException(pce);
}
if (message.isResponse()) {
// fault = new FaultException( "InternalServerError", "" );
// TODO support resourcePath
retVal = new CommMessage(CommMessage.GENERIC_ID, inputId, "/", value, fault);
} else if (!message.isError()) {
// TODO support resourcePath
String opname = doc.getDocumentElement().getFirstChild().getTextContent();
retVal = new CommMessage(CommMessage.GENERIC_ID, opname, "/", value, fault);
}
}
received = true;
return retVal;
}
Aggregations