org.apache.commons.httpclient.methods.FileRequestEntity Java Examples

The following examples show how to use org.apache.commons.httpclient.methods.FileRequestEntity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PropertyIntegrationForceSCAcceptedPropertyTestCase.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Test(groups = "wso2.esb", description = "Testing functionality of FORCE_SC_ACCEPTED " + "- Enabled False")
public void testFORCE_SC_ACCEPTEDPropertyEnabledFalseScenario() throws Exception {
    int responseStatus = 0;

    String strXMLFilename =
            FrameworkPathUtil.getSystemResourceLocation() + "artifacts" + File.separator + "ESB" + File.separator
                    + "mediatorconfig" + File.separator + "property" + File.separator + "GetQuoteRequest.xml";

    File input = new File(strXMLFilename);
    PostMethod post = new PostMethod(getProxyServiceURLHttp("FORCE_SC_ACCEPTED_FalseTestProxy"));
    RequestEntity entity = new FileRequestEntity(input, "text/xml");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", "getQuote");

    HttpClient httpclient = new HttpClient();

    try {
        responseStatus = httpclient.executeMethod(post);
    } finally {
        post.releaseConnection();
    }

    assertEquals(responseStatus, 200, "Response status should be 200");

}
 
Example #2
Source File: PropertyIntegrationForceSCAcceptedPropertyTestCase.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Test(groups = "wso2.esb", description = "Testing functionality of FORCE_SC_ACCEPTED " + "Enabled True  - "
        + "Client should receive 202 message", dependsOnMethods = "testFORCE_SC_ACCEPTEDPropertyEnabledFalseScenario")
public void testWithFORCE_SC_ACCEPTEDPropertyEnabledTrueScenario() throws Exception {
    int responseStatus = 0;

    String strXMLFilename =
            FrameworkPathUtil.getSystemResourceLocation() + "artifacts" + File.separator + "ESB" + File.separator
                    + "mediatorconfig" + File.separator + "property" + File.separator + "PlaceOrder.xml";

    File input = new File(strXMLFilename);
    PostMethod post = new PostMethod(getProxyServiceURLHttp("FORCE_SC_ACCEPTED_TrueTestProxy"));
    RequestEntity entity = new FileRequestEntity(input, "text/xml");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", "placeOrder");

    HttpClient httpclient = new HttpClient();

    try {
        responseStatus = httpclient.executeMethod(post);
    } finally {
        post.releaseConnection();
    }

    assertEquals(responseStatus, 202, "Response status should be 202");
}
 
Example #3
Source File: PropertyIntegrationHTTP_SCTestCase.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Test(groups = "wso2.esb", description = "Getting HTTP status code number ")
public void testHttpResponseCode() throws Exception {

    int responseStatus = 0;

    String strXMLFilename =
            FrameworkPathUtil.getSystemResourceLocation() + "artifacts" + File.separator + "ESB" + File.separator
                    + "mediatorconfig" + File.separator + "property" + File.separator + "GetQuoteRequest.xml";

    File input = new File(strXMLFilename);
    PostMethod post = new PostMethod(getProxyServiceURLHttp("HTTP_SCTestProxy"));
    RequestEntity entity = new FileRequestEntity(input, "text/xml");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", "getQuote");

    HttpClient httpclient = new HttpClient();

    try {
        responseStatus = httpclient.executeMethod(post);
    } finally {
        post.releaseConnection();
    }

    assertEquals(responseStatus, 404, "Response status should be 404");
}
 
Example #4
Source File: S3SignedUrlFileUploader.java    From teamcity-s3-artifact-storage-plugin with Apache License 2.0 6 votes vote down vote up
private void uploadArtifact(@NotNull final String artifactPath, @NotNull final URL uploadUrl, @NotNull final File file, @NotNull final HttpClient awsHttpClient)
  throws IOException {
  try {
    final PutMethod putMethod = new PutMethod(uploadUrl.toString());
    putMethod.addRequestHeader("User-Agent", "TeamCity Agent");
    putMethod.setRequestEntity(new FileRequestEntity(file, S3Util.getContentType(file)));
    HttpClientCloseUtil.executeAndReleaseConnection(awsHttpClient, putMethod);
    LOG.debug(String.format("Successfully uploaded artifact %s to %s", artifactPath, uploadUrl));
  } catch (HttpClientCloseUtil.HttpErrorCodeException e) {
    final String msg;
    if (e.getResponseCode() == HttpStatus.SC_FORBIDDEN) {
      msg = "Failed to upload artifact " + artifactPath + ": received response code HTTP 403. Ensure that the credentials in S3 storage profile are correct.";
    } else {
      msg = "Failed to upload artifact " + artifactPath + " to " + uploadUrl + ": received response code HTTP " + e.getResponseCode() + ".";
    }
    LOG.info(msg);
    throw new IOException(msg);
  }
}
 
Example #5
Source File: PropertyIntegrationHTTP_SCTestCase.java    From product-ei with Apache License 2.0 6 votes vote down vote up
@Test(groups = "wso2.esb", description = "Getting HTTP status code number ")
public void testHttpResponseCode() throws Exception {

    int responseStatus = 0;

    String strXMLFilename = FrameworkPathUtil.getSystemResourceLocation() + "artifacts"
                            + File.separator + "ESB" + File.separator + "mediatorconfig" +
                            File.separator + "property" + File.separator + "GetQuoteRequest.xml";

    File input = new File(strXMLFilename);
    PostMethod post = new PostMethod(getProxyServiceURLHttp("HTTP_SCTestProxy"));
    RequestEntity entity = new FileRequestEntity(input, "text/xml");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", "getQuote");

    HttpClient httpclient = new HttpClient();

    try {
        responseStatus = httpclient.executeMethod(post);
    } finally {
        post.releaseConnection();
    }

    assertEquals(responseStatus, 404, "Response status should be 404");
}
 
Example #6
Source File: MessageWithoutContentTypeTestCase.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Sending a message without mentioning Content Type and check the body part at the listening port
 * <p/>
 * Public JIRA:    WSO2 Carbon/CARBON-6029
 * Responses With No Content-Type Header not handled properly
 * <p/>
 * Test Artifacts: ESB Sample 0
 *
 * @throws Exception   - if the scenario fail
 */
@SetEnvironment(executionEnvironments = {ExecutionEnvironment.ALL})
@Test(groups = "wso2.esb")
public void testMessageWithoutContentType() throws Exception {

    // Get target URL
    String strURL = getMainSequenceURL();
    // Get SOAP action
    String strSoapAction = "getQuote";
    // Get file to be posted
    String strXMLFilename = FrameworkPathUtil.getSystemResourceLocation() + "artifacts" + File.separator +
                            "ESB" + File.separator + "synapseconfig" + File.separator + "messagewithoutcontent" +
                            File.separator + "request.xml";

    File input = new File(strXMLFilename);
    // Prepare HTTP post
    PostMethod post = new PostMethod(strURL);
    // Request content will be retrieved directly
    // from the input stream
    RequestEntity entity = new FileRequestEntity(input, "text/xml");
    post.setRequestEntity(entity);
    // consult documentation for your web service
    post.setRequestHeader("SOAPAction", strSoapAction);
    // Get HTTP client
    HttpClient httpclient = new HttpClient();
    // Execute request
    try {
        int result = httpclient.executeMethod(post);
        // Display status code
        log.info("Response status code: " + result);
        // Display response
        log.info("Response body: ");
        log.info(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are done
        post.releaseConnection();
    }
}
 
Example #7
Source File: ChatRoomDecorator.java    From Spark with Apache License 2.0 5 votes vote down vote up
private void uploadFile(File file, UploadRequest response, ChatRoom room, Message.Type type)
{
    Log.warning("uploadFile request " + room.getRoomJid() + " " + response.putUrl);
    URLConnection urlconnection = null;

    try {
        PutMethod put = new PutMethod(response.putUrl);
        int port = put.getURI().getPort();
        if (port > 0)
        {
            Protocol.registerProtocol( "https", new Protocol( "https", new EasySSLProtocolSocketFactory(), port ) );
        }
        
        HttpClient client = new HttpClient();
        RequestEntity entity = new FileRequestEntity(file, "application/binary");
        put.setRequestEntity(entity);
        put.setRequestHeader("User-Agent", "Spark HttpFileUpload");
        client.executeMethod(put);

        int statusCode = put.getStatusCode();
        String responseBody = put.getResponseBodyAsString();

        Log.warning("uploadFile response " + statusCode + " " + responseBody);

        if ((statusCode >= 200) && (statusCode <= 202))
        {
            broadcastUploadUrl(room.getRoomJid(), response.getUrl, type);
        }

    } catch (Exception e) {
        Log.error("uploadFile error", e);
    }
}
 
Example #8
Source File: Tr069DMAdapter.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void callPutFileApi(String fileType, String url, String filePath, String oui, String prodClass, String version) throws HttpException, IOException, HitDMException {
	org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
	
	PutMethod putMethod = new PutMethod(url);
	File input = new File(filePath);
	RequestEntity entity = new FileRequestEntity(input, "Content-Length: 1431");
	putMethod.setRequestEntity(entity);
	putMethod.setRequestHeader("fileType", fileType);
	putMethod.setRequestHeader("oui", oui);
	putMethod.setRequestHeader("productClass", prodClass);
	putMethod.setRequestHeader("version", version);
	
	client.executeMethod(putMethod);
	
	if (putMethod.getStatusCode() != 200) {
		String debugStr = "DM Server return:"+ putMethod.getStatusCode();
		byte[] body;
		try {
			body = putMethod.getResponseBody();
			if (body != null) {
				debugStr += "\r\nBody:"+new String(body);
			}
		} catch (Exception e) {
			debugStr += e.getMessage();
		}
		
		throw new HitDMException(putMethod.getStatusCode(), debugStr);
	}
	
}
 
Example #9
Source File: Tr069DMAdapter.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void callPutFileApi(String fileType, String url, String filePath, String oui, String prodClass, String version) throws HttpException, IOException, HitDMException {
	org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
	
	PutMethod putMethod = new PutMethod(url);
	File input = new File(filePath);
	RequestEntity entity = new FileRequestEntity(input, "Content-Length: 1431");
	putMethod.setRequestEntity(entity);
	putMethod.setRequestHeader("fileType", fileType);
	putMethod.setRequestHeader("oui", oui);
	putMethod.setRequestHeader("productClass", prodClass);
	putMethod.setRequestHeader("version", version);
	
	client.executeMethod(putMethod);
	
	if (putMethod.getStatusCode() != 200) {
		String debugStr = "DM Server return:"+ putMethod.getStatusCode();
		byte[] body;
		try {
			body = putMethod.getResponseBody();
			if (body != null) {
				debugStr += "\r\nBody:"+new String(body);
			}
		} catch (Exception e) {
			debugStr += e.getMessage();
		}
		
		throw new HitDMException(putMethod.getStatusCode(), debugStr);
	}
	
}
 
Example #10
Source File: PropertyIntegrationForceSCAcceptedPropertyTestCase.java    From product-ei with Apache License 2.0 5 votes vote down vote up
@Test(groups = "wso2.esb", description = "Testing functionality of FORCE_SC_ACCEPTED " +
                                         "Enabled True  - " +
                                         "Client should receive 202 message",
      dependsOnMethods = "testFORCE_SC_ACCEPTEDPropertyEnabledFalseScenario")
public void testWithFORCE_SC_ACCEPTEDPropertyEnabledTrueScenario() throws Exception {
    verifyProxyServiceExistence("FORCE_SC_ACCEPTED_TrueTestProxy");

    int responseStatus = 0;

    String strXMLFilename = FrameworkPathUtil.getSystemResourceLocation() + "artifacts"
                            + File.separator + "ESB" + File.separator + "mediatorconfig" +
                            File.separator + "property" + File.separator + "PlaceOrder.xml";

    File input = new File(strXMLFilename);
    PostMethod post = new PostMethod(getProxyServiceURLHttp("FORCE_SC_ACCEPTED_TrueTestProxy"));
    RequestEntity entity = new FileRequestEntity(input, "text/xml");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", "placeOrder");

    HttpClient httpclient = new HttpClient();

    try {
        responseStatus = httpclient.executeMethod(post);
    } finally {
        post.releaseConnection();
    }

    assertEquals(responseStatus, 202, "Response status should be 202");
}
 
Example #11
Source File: PropertyIntegrationForceSCAcceptedPropertyTestCase.java    From product-ei with Apache License 2.0 5 votes vote down vote up
@Test(groups = "wso2.esb", description = "Testing functionality of FORCE_SC_ACCEPTED " +
                                         "- Enabled False")
public void testFORCE_SC_ACCEPTEDPropertyEnabledFalseScenario() throws Exception {
    verifyProxyServiceExistence("FORCE_SC_ACCEPTED_FalseTestProxy");

    int responseStatus = 0;

    String strXMLFilename = FrameworkPathUtil.getSystemResourceLocation() + "artifacts"
                            + File.separator + "ESB" + File.separator + "mediatorconfig" +
                            File.separator + "property" + File.separator + "GetQuoteRequest.xml";

    File input = new File(strXMLFilename);
    PostMethod post = new PostMethod(getProxyServiceURLHttp("FORCE_SC_ACCEPTED_FalseTestProxy"));
    RequestEntity entity = new FileRequestEntity(input, "text/xml");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", "getQuote");

    HttpClient httpclient = new HttpClient();

    try {
        responseStatus = httpclient.executeMethod(post);
    } finally {
        post.releaseConnection();
    }

    assertEquals(responseStatus, 200, "Response status should be 200");

}
 
Example #12
Source File: ESBJAVA_4118_SOAPHeaderHandlingTest.java    From product-ei with Apache License 2.0 5 votes vote down vote up
@Test(groups = "wso2.esb", description = "Test whether the callout mediator successfully handle SOAP messages " +
        "Having SOAP header")
public void testSOAPHeaderHandling () throws Exception{
    String endpoint = "http://localhost:8480/services/TestCalloutSoapHeader";
    String soapRequest = TestConfigurationProvider.getResourceLocation() + "artifacts" + File.separator +
            "ESB" + File.separator + "mediatorconfig" + File.separator + "callout" +
            File.separator + "SOAPRequestWithHeader.xml";
    File input = new File(soapRequest);
    PostMethod post = new PostMethod(endpoint);
    RequestEntity entity = new FileRequestEntity(input, "text/xml");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction","getQuote");
    HttpClient httpClient = new HttpClient();
    boolean errorLog = false;

    try {
        int result = httpClient.executeMethod(post);
        String responseBody = post.getResponseBodyAsString();
        log.info("Response Status: " + result);
        log.info("Response Body: "+ responseBody);

        LogEvent[] logs = logViewerClient.getAllRemoteSystemLogs();
        for (LogEvent logEvent : logs) {
            if (logEvent.getPriority().equals("ERROR")) {
                String message = logEvent.getMessage();
                if (message.contains("Unable to convert to SoapHeader Block")) {
                    errorLog = true;
                    break;
                }
            }
        }
    } finally {
        post.releaseConnection();
    }
    assertFalse(errorLog, "Mediator Hasn't invoked successfully.");
}
 
Example #13
Source File: ESBJAVA_4239_HTTP_SC_HandlingTests.java    From product-ei with Apache License 2.0 5 votes vote down vote up
@Test(groups = "wso2.esb", description = "Test whether response HTTP status code getting correctly after callout " +
                                         "mediator successfully execute")
public void testHttpStatusCodeGettingSuccessfully() throws Exception {
    String endpoint = getProxyServiceURLHttp("TestCalloutHTTP_SC");
    String soapRequest = TestConfigurationProvider.getResourceLocation() + "artifacts" + File.separator +
                         "ESB" + File.separator + "mediatorconfig" + File.separator + "callout" +
                         File.separator + "SOAPRequestWithHeader.xml";
    File input = new File(soapRequest);
    PostMethod post = new PostMethod(endpoint);
    RequestEntity entity = new FileRequestEntity(input, "text/xml");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", "getQuote");
    HttpClient httpClient = new HttpClient();
    boolean errorLog = false;

    try {
        httpClient.executeMethod(post);

        LogEvent[] logs = logViewerClient.getAllRemoteSystemLogs();
        for (LogEvent logEvent : logs) {
            if (logEvent.getPriority().equals("INFO")) {
                String message = logEvent.getMessage();
                if (message.contains("STATUS-Fault") && message.contains("404 Error: Not Found")) {
                    errorLog = true;
                    break;
                }
            }
        }
    } finally {
        post.releaseConnection();
    }

    if (!errorLog) {
        fail("Response HTTP code not return successfully.");
    }
}
 
Example #14
Source File: MessageWithoutContentTypeTestCase.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Sending a message without mentioning Content Type and check the body part at the listening port
 * <p/>
 * Public JIRA:    WSO2 Carbon/CARBON-6029
 * Responses With No Content-Type Header not handled properly
 * <p/>
 * Test Artifacts: ESB Sample 0
 *
 * @throws Exception - if the scenario fail
 */
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.ALL })
@Test(groups = "wso2.esb")
public void testMessageWithoutContentType() throws Exception {

    // Get target URL
    String strURL = getMainSequenceURL();
    // Get SOAP action
    String strSoapAction = "getQuote";
    // Get file to be posted
    String strXMLFilename =
            FrameworkPathUtil.getSystemResourceLocation() + "artifacts" + File.separator + "ESB" + File.separator
                    + "synapseconfig" + File.separator + "messagewithoutcontent" + File.separator + "request.xml";

    File input = new File(strXMLFilename);
    // Prepare HTTP post
    PostMethod post = new PostMethod(strURL);
    // Request content will be retrieved directly
    // from the input stream
    RequestEntity entity = new FileRequestEntity(input, "text/xml");
    post.setRequestEntity(entity);
    // consult documentation for your web service
    post.setRequestHeader("SOAPAction", strSoapAction);
    // Get HTTP client
    HttpClient httpclient = new HttpClient();
    // Execute request
    try {
        int result = httpclient.executeMethod(post);
        // Display status code
        log.info("Response status code: " + result);
        // Display response
        log.info("Response body: ");
        log.info(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are done
        post.releaseConnection();
    }
}
 
Example #15
Source File: MessageWithoutContentTypeTestCase.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Sending a message without mentioning Content Type and check the body part at the listening port
 * <p/>
 * Public JIRA:    WSO2 Carbon/CARBON-6029
 * Responses With No Content-Type Header not handled properly
 * <p/>
 * Test Artifacts: ESB Sample 0
 *
 * @throws Exception   - if the scenario fail
 */
@SetEnvironment(executionEnvironments = {ExecutionEnvironment.ALL})
@Test(groups = "wso2.esb")
public void testMessageWithoutContentType() throws Exception {

    // Get target URL
    String strURL = getMainSequenceURL();
    // Get SOAP action
    String strSoapAction = "getQuote";
    // Get file to be posted
    String strXMLFilename = FrameworkPathUtil.getSystemResourceLocation() + "artifacts" + File.separator +
            "ESB" + File.separator + "synapseconfig" + File.separator + "messagewithoutcontent" +
            File.separator + "request.xml";

    File input = new File(strXMLFilename);
    // Prepare HTTP post
    PostMethod post = new PostMethod(strURL);
    // Request content will be retrieved directly
    // from the input stream
    RequestEntity entity = new FileRequestEntity(input, "text/xml");
    post.setRequestEntity(entity);
    // consult documentation for your web service
    post.setRequestHeader("SOAPAction", strSoapAction);
    // Get HTTP client
    HttpClient httpclient = new HttpClient();
    // Execute request
    try {
        int result = httpclient.executeMethod(post);
        // Display status code
        log.info("Response status code: " + result);
        // Display response
        log.info("Response body: ");
        log.info(post.getResponseBodyAsString());
    } finally {
        // Release current connection to the connection pool once you are done
        post.releaseConnection();
    }
}
 
Example #16
Source File: ESBJAVA_4118_SOAPHeaderHandlingTest.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
@Test(groups = "wso2.esb", description = "Test whether the callout mediator successfully handle SOAP messages "
        + "Having SOAP header")
public void testSOAPHeaderHandling() throws Exception {
    String endpoint = "http://localhost:8480/services/TestCalloutSoapHeader";
    String soapRequest =
            TestConfigurationProvider.getResourceLocation() + "artifacts" + File.separator + "ESB" + File.separator
                    + "mediatorconfig" + File.separator + "callout" + File.separator + "SOAPRequestWithHeader.xml";
    File input = new File(soapRequest);
    PostMethod post = new PostMethod(endpoint);
    RequestEntity entity = new FileRequestEntity(input, "text/xml");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", "getQuote");
    HttpClient httpClient = new HttpClient();
    boolean errorLog = false;

    try {
        int result = httpClient.executeMethod(post);
        String responseBody = post.getResponseBodyAsString();
        log.info("Response Status: " + result);
        log.info("Response Body: " + responseBody);

        errorLog = carbonLogReader.checkForLog("Unable to convert to SoapHeader Block", DEFAULT_TIMEOUT);
        carbonLogReader.stop();
    } finally {
        post.releaseConnection();
    }
    assertFalse(errorLog, "Mediator Hasn't invoked successfully.");
}
 
Example #17
Source File: ESBJAVA_4239_HTTP_SC_HandlingTests.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
@Test(groups = "wso2.esb", description = "Test whether response HTTP status code getting correctly after callout "
        + "mediator successfully execute")
public void testHttpStatusCodeGettingSuccessfully() throws Exception {
    String endpoint = getProxyServiceURLHttp("TestCalloutHTTP_SC");
    String soapRequest =
            TestConfigurationProvider.getResourceLocation() + "artifacts" + File.separator + "ESB" + File.separator
                    + "mediatorconfig" + File.separator + "callout" + File.separator + "SOAPRequestWithHeader.xml";
    File input = new File(soapRequest);
    PostMethod post = new PostMethod(endpoint);
    RequestEntity entity = new FileRequestEntity(input, "text/xml");
    post.setRequestEntity(entity);
    post.setRequestHeader("SOAPAction", "getQuote");
    HttpClient httpClient = new HttpClient();
    boolean errorLog = false;

    try {
        httpClient.executeMethod(post);
        errorLog = carbonLogReader.checkForLog("STATUS-Fault", DEFAULT_TIMEOUT) && carbonLogReader.
                checkForLog("404 Error: Not Found", DEFAULT_TIMEOUT);
        carbonLogReader.stop();
    } finally {
        post.releaseConnection();
    }

    if (!errorLog) {
        fail("Response HTTP code not return successfully.");
    }
}