use of org.apache.axis2.util.URL in project wso2-synapse by wso2.
the class RESTUtil method getURI.
/**
* This method will return the URI part for the GET HTTPRequest by converting
* the SOAP infoset to the URL-encoded GET format
*
* @param messageContext - from which the SOAP infoset will be extracted to encode
* @param address - address of the actual service
* @return uri - ERI of the GET request
* @throws AxisFault - if the SOAP infoset cannot be converted in to the GET URL-encoded format
*/
public static String getURI(MessageContext messageContext, String address) throws AxisFault {
OMElement firstElement;
address = address.substring(address.indexOf("//") + 2);
address = address.substring(address.indexOf("/"));
String queryParameterSeparator = (String) messageContext.getProperty(WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR);
if (queryParameterSeparator == null) {
queryParameterSeparator = WSDL20DefaultValueHolder.getDefaultValue(WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR);
}
firstElement = messageContext.getEnvelope().getBody().getFirstElement();
String params = "";
if (firstElement != null) {
// first element corresponds to the operation name
address = address + "/" + firstElement.getLocalName();
} else {
firstElement = messageContext.getEnvelope().getBody();
}
Iterator iter = firstElement.getChildElements();
String legalCharacters = WSDL2Constants.LEGAL_CHARACTERS_IN_QUERY.replaceAll(queryParameterSeparator, "");
StringBuffer buff = new StringBuffer(params);
// iterate through the child elements and find the request parameters
while (iter.hasNext()) {
OMElement element = (OMElement) iter.next();
try {
buff.append(URIEncoderDecoder.quoteIllegal(element.getLocalName(), legalCharacters)).append("=").append(URIEncoderDecoder.quoteIllegal(element.getText(), legalCharacters)).append(queryParameterSeparator);
} catch (UnsupportedEncodingException e) {
throw new AxisFault("URI Encoding error : " + element.getLocalName() + "=" + element.getText(), e);
}
}
params = buff.toString();
if (params.trim().length() != 0) {
int index = address.indexOf("?");
if (index == -1) {
address = address + "?" + params.substring(0, params.length() - 1);
} else if (index == address.length() - 1) {
address = address + params.substring(0, params.length() - 1);
} else {
address = address + queryParameterSeparator + params.substring(0, params.length() - 1);
}
}
return address;
}
use of org.apache.axis2.util.URL in project wso2-synapse by wso2.
the class JSONClient method executeClient.
public static void executeClient() throws Exception {
Options options = new Options();
ServiceClient serviceClient;
ConfigurationContext configContext = null;
String addUrl = getProperty("addurl", "http://localhost:8280/services/JSONProxy");
String trpUrl = getProperty("trpurl", null);
String prxUrl = getProperty("prxurl", null);
String repo = getProperty("repository", "client_repo");
String symbol = getProperty("symbol", "IBM");
if (repo != null && !"null".equals(repo)) {
configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(repo, repo + File.separator + "conf" + File.separator + "axis2.xml");
serviceClient = new ServiceClient(configContext, null);
} else {
serviceClient = new ServiceClient();
}
if (trpUrl != null && !"null".equals(trpUrl)) {
options.setProperty(Constants.Configuration.TRANSPORT_URL, trpUrl);
}
if (prxUrl != null && !"null".equals(prxUrl)) {
HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties.ProxyProperties();
URL url = new URL(prxUrl);
proxyProperties.setProxyName(url.getHost());
proxyProperties.setProxyPort(url.getPort());
proxyProperties.setUserName("");
proxyProperties.setPassWord("");
proxyProperties.setDomain("");
options.setProperty(HTTPConstants.PROXY, proxyProperties);
}
serviceClient.engageModule("addressing");
options.setTo(new EndpointReference(addUrl));
options.setAction("urn:getQuote");
options.setProperty(Constants.Configuration.MESSAGE_TYPE, "application/json");
serviceClient.setOptions(options);
OMElement payload = AXIOMUtil.stringToOM("<getQuote><request><symbol>" + symbol + "</symbol>" + "</request></getQuote>");
OMElement response = serviceClient.sendReceive(payload);
if (response.getLocalName().equals("getQuoteResponse")) {
OMElement last = response.getFirstElement().getFirstChildWithName(new QName("last"));
System.out.println("Standard :: Stock price = $" + last.getText());
} else {
throw new Exception("Unexpected response : " + response);
}
Thread.sleep(1000);
if (configContext != null) {
configContext.terminate();
}
}
use of org.apache.axis2.util.URL in project wso2-synapse by wso2.
the class Axis2HttpTransportSender method invoke.
@Override
public InvocationResponse invoke(MessageContext msgCtx) throws AxisFault {
if (AddressingHelper.isReplyRedirected(msgCtx)) {
msgCtx.setProperty(BridgeConstants.IGNORE_SC_ACCEPTED, BridgeConstants.VALUE_TRUE);
}
EndpointReference destinationEPR = RequestResponseUtils.getDestinationEPR(msgCtx);
if (isRequestToBackend(destinationEPR)) {
try {
URL destinationURL = new URL(destinationEPR.getAddress());
sendRequestToBackendService(msgCtx, destinationURL);
} catch (MalformedURLException e) {
handleException("Malformed URL in the target EPR", e);
} catch (IOException e) {
handleException("Error while sending the request to the backend service " + destinationEPR.getAddress(), e);
}
} else {
HttpCarbonMessage clientRequest = (HttpCarbonMessage) msgCtx.getProperty(BridgeConstants.HTTP_CLIENT_REQUEST_CARBON_MESSAGE);
if (clientRequest == null) {
LOG.warn("Unable to find the original client request to send the response");
return InvocationResponse.ABORT;
}
try {
sendResponseToClient(msgCtx, clientRequest);
if (msgCtx.getOperationContext() != null) {
msgCtx.getOperationContext().setProperty(Constants.RESPONSE_WRITTEN, Constants.VALUE_TRUE);
}
} catch (IOException e) {
handleException("Error occurred while sending a response to the client", e);
}
}
return InvocationResponse.CONTINUE;
}
use of org.apache.axis2.util.URL in project wso2-synapse by wso2.
the class TargetRequestHandler method createHttpClient.
public static HttpClientConnector createHttpClient(URL url, MessageContext msgContext, HttpWsConnectorFactory httpWsConnectorFactory, ConnectionManager connectionManager, BootstrapConfiguration bootstrapConfiguration, TargetConfiguration targetConfiguration) throws AxisFault {
try {
SenderConfiguration senderConfiguration = new SenderConfiguration();
populateSenderConfigurations(msgContext, senderConfiguration, targetConfiguration, url);
return httpWsConnectorFactory.createHttpClientConnector(bootstrapConfiguration, senderConfiguration, connectionManager);
} catch (Exception ex) {
throw new AxisFault("Error while creating the HTTP Client Connector. ", ex);
}
}
use of org.apache.axis2.util.URL in project wso2-synapse by wso2.
the class TargetRequestHandler method getOutboundReqPath.
private static String getOutboundReqPath(URL url, MessageContext msgCtx) throws IOException {
if (HttpUtils.isGETRequest(msgCtx) || (RelayUtils.isDeleteRequestWithoutPayload(msgCtx))) {
MessageFormatter formatter = MessageProcessorSelector.getMessageFormatter(msgCtx);
OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(msgCtx);
if (formatter != null) {
URL targetURL = formatter.getTargetAddress(msgCtx, format, url);
if (targetURL != null && !targetURL.toString().isEmpty()) {
if (msgCtx.isPropertyTrue(BridgeConstants.POST_TO_URI)) {
return targetURL.toString();
} else {
return targetURL.getPath() + ((targetURL.getQuery() != null && !targetURL.getQuery().isEmpty()) ? ("?" + targetURL.getQuery()) : "");
}
}
}
}
if (msgCtx.isPropertyTrue(BridgeConstants.POST_TO_URI)) {
return url.toString();
}
// TODO: need to check "(route.getProxyHost() != null && !route.isTunnelled())" as well
return msgCtx.isPropertyTrue(BridgeConstants.FULL_URI) ? url.toString() : url.getPath() + (url.getQuery() != null ? "?" + url.getQuery() : "");
}
Aggregations