use of org.apache.axis2.util.URL in project wso2-synapse by wso2.
the class PassThroughNHttpGetProcessor method generateXsd.
/**
* Generates Schema.
*
* @param response
* HttpResponse
* @param msgContext
* Current MessageContext
* @param conn
* NHttpServerConnection
* @param os
* OutputStream
* @param serviceName
* service name
* @param parameters
* url parameters
*/
protected void generateXsd(HttpResponse response, MessageContext msgContext, NHttpServerConnection conn, OutputStream os, String serviceName, Map<String, String> parameters) {
if (parameters.get("xsd") == null || "".equals(parameters.get("xsd"))) {
AxisService service = cfgCtx.getAxisConfiguration().getServices().get(serviceName);
if (service != null) {
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
service.printSchema(output);
response.addHeader(CONTENT_TYPE, TEXT_XML);
sendResponseAndFinish(response, output.toByteArray(), conn, os, msgContext);
} catch (Exception e) {
handleBrowserException(response, msgContext, conn, os, "Error generating ?xsd output for service : " + serviceName, e);
}
} else {
msgContext.setProperty(PassThroughConstants.REST_GET_DELETE_INVOKE, true);
}
} else {
// cater for named XSDs - check for the xsd name
String schemaName = parameters.get("xsd");
AxisService service = cfgCtx.getAxisConfiguration().getServices().get(serviceName);
if (service != null) {
// run the population logic just to be sure
service.populateSchemaMappings();
// write out the correct schema
Map schemaTable = service.getSchemaMappingTable();
XmlSchema schema = (XmlSchema) schemaTable.get(schemaName);
if (schema == null) {
int dotIndex = schemaName.indexOf('.');
if (dotIndex > 0) {
String schemaKey = schemaName.substring(0, dotIndex);
schema = (XmlSchema) schemaTable.get(schemaKey);
}
}
// schema found - write it to the stream
if (schema != null) {
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
schema.write(output);
response.addHeader(CONTENT_TYPE, TEXT_XML);
sourceHandler.commitResponseHideExceptions(conn, response);
write(conn, os, output.toByteArray());
closeOutputStream(os);
msgContext.setProperty(GET_REQUEST_HANDLED, Boolean.TRUE);
} catch (Exception e) {
handleBrowserException(response, msgContext, conn, os, "Error generating named ?xsd output for service : " + serviceName, e);
}
} else {
// no schema available by that name - send 404
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
closeOutputStream(os);
msgContext.setProperty(GET_REQUEST_HANDLED, Boolean.TRUE);
}
} else {
msgContext.setProperty(PassThroughConstants.REST_GET_DELETE_INVOKE, true);
}
}
}
use of org.apache.axis2.util.URL in project wso2-synapse by wso2.
the class TargetHandler method logException.
private void logException(NHttpClientConnection conn, Exception e) {
ProtocolState state = TargetContext.getState(conn);
if (state != null) {
MessageContext requestMsgCtx = TargetContext.get(conn).getRequestMsgCtx();
Map<String, String> logDetails = getLoggingInfo(conn, state, requestMsgCtx, e);
log.warn("ERROR_CODE = " + logDetails.get("error_code") + ", STATE_DESCRIPTION = Exception occurred " + logDetails.get("state_description") + ", INTERNAL_STATE = " + state + ", DIRECTION = " + logDetails.get("direction") + ", " + "CAUSE_OF_ERROR = " + logDetails.get("cause_of_error") + ", TARGET_HOST = " + logDetails.get("host") + ", TARGET_PORT = " + logDetails.get("port") + ", TARGET_CONTEXT = " + logDetails.get("url") + ", " + "HTTP_METHOD = " + logDetails.get("method") + ", TRIGGER_TYPE = " + logDetails.get("trigger_type") + ", TRIGGER_NAME = " + logDetails.get("trigger_name") + ", REMOTE_ADDRESS = " + getBackEndConnectionInfo(conn) + ", " + "CONNECTION = " + conn);
}
}
use of org.apache.axis2.util.URL in project wso2-synapse by wso2.
the class TargetHandler method closed.
public void closed(NHttpClientConnection conn) {
ProtocolState state = TargetContext.getState(conn);
MessageContext requestMsgCtx = TargetContext.get(conn).getRequestMsgCtx();
Map<String, String> logDetails = getLoggingInfo(conn, state, requestMsgCtx);
boolean isFault = false;
if (log.isDebugEnabled()) {
log.debug("Connection closed by target host while in state " + state.name() + ". Response code : " + conn.getStatus());
}
if (state == ProtocolState.RESPONSE_DONE || state == ProtocolState.REQUEST_READY) {
if (log.isDebugEnabled()) {
log.debug("Keep-Alive Connection closed " + getConnectionLoggingInfo(conn));
}
} else if (state == ProtocolState.REQUEST_HEAD || state == ProtocolState.REQUEST_BODY) {
informWriterError(conn);
log.warn("ERROR_CODE = " + ErrorCodes.CONNECTION_CLOSED + ", STATE_DESCRIPTION = Connection closed by " + "target host " + logDetails.get("state_description") + ", INTERNAL_STATE = " + state + ", DIRECTION = " + logDetails.get("direction") + ", " + "CAUSE_OF_ERROR = Connection between the Server and the BackEnd has been closed, TARGET_HOST = " + logDetails.get("host") + ", TARGET_PORT = " + logDetails.get("port") + ", TARGET_CONTEXT = " + logDetails.get("url") + ", " + "HTTP_METHOD = " + logDetails.get("method") + ", TRIGGER_TYPE = " + logDetails.get("trigger_type") + ", TRIGGER_NAME = " + logDetails.get("trigger_name") + ", REMOTE_ADDRESS = " + getBackEndConnectionInfo(conn) + ", " + "CONNECTION = " + conn);
isFault = true;
} else if (state == ProtocolState.RESPONSE_HEAD || state == ProtocolState.RESPONSE_BODY) {
informReaderError(conn);
log.warn("ERROR_CODE = " + ErrorCodes.CONNECTION_CLOSED + ", STATE_DESCRIPTION = Connection closed by " + "target host " + logDetails.get("state_description") + ", INTERNAL_STATE = " + state + ", DIRECTION = " + logDetails.get("direction") + ", " + "CAUSE_OF_ERROR = Connection between the Server and the BackEnd has been closed, TARGET_HOST = " + logDetails.get("host") + ", TARGET_PORT = " + logDetails.get("port") + ", TARGET_CONTEXT = " + logDetails.get("url") + ", " + "HTTP_METHOD = " + logDetails.get("method") + ", TRIGGER_TYPE = " + logDetails.get("trigger_type") + ", TRIGGER_NAME = " + logDetails.get("trigger_name") + ", REMOTE_ADDRESS = " + getBackEndConnectionInfo(conn) + ", " + "CONNECTION = " + conn);
isFault = true;
} else if (state == ProtocolState.REQUEST_DONE) {
log.warn("ERROR_CODE = " + ErrorCodes.CONNECTION_CLOSED + ", STATE_DESCRIPTION = Connection closed by " + "target host " + logDetails.get("state_description") + ", INTERNAL_STATE = " + state + ", DIRECTION = " + logDetails.get("direction") + ", " + "CAUSE_OF_ERROR = Connection between the Server and the BackEnd has been closed, TARGET_HOST = " + logDetails.get("host") + ", TARGET_PORT = " + logDetails.get("port") + ", TARGET_CONTEXT = " + logDetails.get("url") + ", " + "HTTP_METHOD = " + logDetails.get("method") + ", TRIGGER_TYPE = " + logDetails.get("trigger_type") + ", TRIGGER_NAME = " + logDetails.get("trigger_name") + ", REMOTE_ADDRESS = " + getBackEndConnectionInfo(conn) + ", " + "CONNECTION = " + conn);
isFault = true;
}
if (isFault) {
if (requestMsgCtx != null) {
requestMsgCtx.setProperty(PassThroughConstants.INTERNAL_EXCEPTION_ORIGIN, PassThroughConstants.INTERNAL_ORIGIN_ERROR_HANDLER);
targetErrorHandler.handleError(requestMsgCtx, ErrorCodes.CONNECTION_CLOSED, "Error in Sender", null, state);
}
}
metrics.disconnected();
TargetContext.updateState(conn, ProtocolState.CLOSED);
targetConfiguration.getConnections().shutdownConnection(conn, isFault);
}
use of org.apache.axis2.util.URL in project wso2-synapse by wso2.
the class TargetRequestFactory method create.
public static TargetRequest create(MessageContext msgContext, HttpRoute route, TargetConfiguration configuration) throws AxisFault {
try {
String httpMethod = (String) msgContext.getProperty(Constants.Configuration.HTTP_METHOD);
if (httpMethod == null) {
httpMethod = "POST";
}
// basic request
Boolean noEntityBody = (Boolean) msgContext.getProperty(PassThroughConstants.NO_ENTITY_BODY);
if (msgContext.getEnvelope().getBody().getFirstElement() != null) {
noEntityBody = false;
}
EndpointReference epr = PassThroughTransportUtils.getDestinationEPR(msgContext);
URL url = new URL(epr.getAddress());
TargetRequest request = new TargetRequest(configuration, route, url, httpMethod, noEntityBody == null || !noEntityBody);
// otherwise Host header will not replaced after first call
if (msgContext.getProperty(NhttpConstants.REQUEST_HOST_HEADER) != null) {
Object headers = msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
if (headers != null) {
Map headersMap = (Map) headers;
if (!headersMap.containsKey(HTTPConstants.HEADER_HOST)) {
headersMap.put(HTTPConstants.HEADER_HOST, msgContext.getProperty(NhttpConstants.REQUEST_HOST_HEADER));
}
}
}
// headers
PassThroughTransportUtils.removeUnwantedHeaders(msgContext, configuration);
Object o = msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
if (o != null && o instanceof Map) {
Map headers = (Map) o;
for (Object entryObj : headers.entrySet()) {
Map.Entry entry = (Map.Entry) entryObj;
if (entry.getValue() != null && entry.getKey() instanceof String && entry.getValue() instanceof String) {
if (HTTPConstants.HEADER_HOST.equalsIgnoreCase((String) entry.getKey()) && !configuration.isPreserveHttpHeader(HTTPConstants.HEADER_HOST)) {
if (msgContext.getProperty(NhttpConstants.REQUEST_HOST_HEADER) != null) {
request.addHeader((String) entry.getKey(), (String) msgContext.getProperty(NhttpConstants.REQUEST_HOST_HEADER));
}
} else {
request.addHeader((String) entry.getKey(), (String) entry.getValue());
}
}
}
}
String cType = getContentType(msgContext, configuration.isPreserveHttpHeader(HTTP.CONTENT_TYPE));
if (cType != null && !httpMethod.equals(HTTPConstants.HTTP_METHOD_GET) && RelayUtils.shouldOverwriteContentType(msgContext, request)) {
if (!TargetRequestFactory.isMultipartContent(cType) && msgContext.isDoingSwA()) {
cType = HTTPConstants.MEDIA_TYPE_MULTIPART_RELATED;
}
String messageType = (String) msgContext.getProperty(NhttpConstants.MESSAGE_TYPE);
if (messageType != null) {
boolean builderInvoked = false;
final Pipe pipe = (Pipe) msgContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
if (pipe != null) {
builderInvoked = Boolean.TRUE.equals(msgContext.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED));
}
// skip of setting formatter specific content Type
if (messageType.indexOf(HTTPConstants.MEDIA_TYPE_MULTIPART_RELATED) == -1 && messageType.indexOf(HTTPConstants.MEDIA_TYPE_MULTIPART_FORM_DATA) == -1) {
Map msgCtxheaders = (Map) o;
if (msgCtxheaders != null && !cType.isEmpty()) {
msgCtxheaders.put(HTTP.CONTENT_TYPE, cType);
}
request.addHeader(HTTP.CONTENT_TYPE, cType);
}
// boundary related content type at Content-Type header
if (builderInvoked && (((messageType.indexOf(HTTPConstants.MEDIA_TYPE_MULTIPART_RELATED) != -1) || (messageType.indexOf(HTTPConstants.MEDIA_TYPE_MULTIPART_FORM_DATA) != -1)))) {
request.addHeader(HTTP.CONTENT_TYPE, cType);
}
} else {
request.addHeader(HTTP.CONTENT_TYPE, cType);
}
}
// version
String forceHttp10 = (String) msgContext.getProperty(PassThroughConstants.FORCE_HTTP_1_0);
if ("true".equals(forceHttp10)) {
request.setVersion(HttpVersion.HTTP_1_0);
}
// keep alive
String noKeepAlive = (String) msgContext.getProperty(PassThroughConstants.NO_KEEPALIVE);
if ("true".equals(noKeepAlive) || PassThroughConfiguration.getInstance().isKeepAliveDisabled()) {
request.setKeepAlive(false);
}
// port
int port = url.getPort();
request.setPort(port);
// chunk
String disableChunking = (String) msgContext.getProperty(PassThroughConstants.DISABLE_CHUNKING);
if ("true".equals(disableChunking)) {
request.setChunk(false);
}
// full url
String fullUrl = (String) msgContext.getProperty(PassThroughConstants.FULL_URI);
if ("true".equals(fullUrl)) {
request.setFullUrl(true);
}
// Add excess respsonse header.
String excessProp = NhttpConstants.EXCESS_TRANSPORT_HEADERS;
Map excessHeaders = (Map) msgContext.getProperty(excessProp);
if (excessHeaders != null) {
for (Iterator iterator = excessHeaders.keySet().iterator(); iterator.hasNext(); ) {
String key = (String) iterator.next();
for (String excessVal : (Collection<String>) excessHeaders.get(key)) {
request.addHeader(key, (String) excessVal);
}
}
}
return request;
} catch (MalformedURLException e) {
handleException("Invalid to address" + msgContext.getTo().getAddress(), e);
}
return null;
}
use of org.apache.axis2.util.URL in project wso2-synapse by wso2.
the class CalloutMediator method init.
public void init(SynapseEnvironment synEnv) {
try {
configCtx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(clientRepository != null ? clientRepository : DEFAULT_CLIENT_REPO, axis2xml != null ? axis2xml : DEFAULT_AXIS2_XML);
if (serviceURL != null) {
serviceURL = changeEndPointReference(serviceURL);
}
blockingMsgSender = new BlockingMsgSender();
blockingMsgSender.setConfigurationContext(configCtx);
blockingMsgSender.init();
EndpointDefinition endpointDefinition = null;
if (serviceURL != null) {
// If Service URL is specified, it is given the highest priority
endpoint = new AddressEndpoint();
endpointDefinition = new EndpointDefinition();
endpointDefinition.setAddress(serviceURL);
endpointDefinition.addSuspendErrorCode(-1);
endpointDefinition.addTimeoutErrorCode(-1);
((AddressEndpoint) endpoint).setDefinition(endpointDefinition);
isWrappingEndpointCreated = true;
} else if (endpoint == null && endpointKey == null) {
// Use a default endpoint in this case - i.e. the To header
endpoint = new DefaultEndpoint();
endpointDefinition = new EndpointDefinition();
((DefaultEndpoint) endpoint).setDefinition(endpointDefinition);
isWrappingEndpointCreated = true;
}
if (endpointDefinition != null && isSecurityOn()) {
endpointDefinition.setSecurityOn(true);
if (wsSecPolicyKey != null) {
endpointDefinition.setWsSecPolicyKey(wsSecPolicyKey);
} else {
if (inboundWsSecPolicyKey != null) {
endpointDefinition.setInboundWsSecPolicyKey(inboundWsSecPolicyKey);
}
if (outboundWsSecPolicyKey != null) {
endpointDefinition.setOutboundWsSecPolicyKey(outboundWsSecPolicyKey);
}
}
}
if (endpoint != null) {
endpoint.init(synEnv);
}
} catch (AxisFault e) {
String msg = "Error initializing callout mediator : " + e.getMessage();
log.error(msg, e);
throw new SynapseException(msg, e);
}
}
Aggregations