Hello guys,
I am currently working on quite exciting assignment. Here is what I want to
do:
@GET REST response -> CXF to get parameters from exchange -> 3rd Party API
to get session_id -> BEAN to send data via POST method and in
multipart/mixed( it is for uploading files )
My first problem is that I don't know how to set the REST, for now it looks
like this:
@GET
@Path("/{myServerHost}/upload")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@PathParam("myHost") String knowledgetreeHost,
@QueryParam("myPort") String knowledgetreePort,
@QueryParam("user")
String user,
@QueryParam("password") String password,
@QueryParam("file") String file)
{
return null;
}
Then I have route and I am setting http method to post
.when(header(CxfConstants.OPERATION_NAME).isEqualTo("uploadFile"))
.recipientList("https://myserver.com/service/?method=login&username=myusername&password=123456")
.beanRef("myBean", "getSessionId")
// For now I needed data in GET(session_id),it is no problem, but in
the following method
// I need to send POST request
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.beanRef("myBean","uploadMyFile")
.endChoice()
Method uploadMyFile should send POST request. For now it looks like this:
public void uploadMyFile(Exchange exchange) throws IOException {
//SESSION_ID is set in previous steps of route
String sessionId =
exchange.getProperty("SESSION_ID").toString();
//FILENAME is taken from URL
/**
String filename = (String) msgList.get(4);
exchange.setProperty("FILENAME", filename);
*/
String filename =
"@"+exchange.getProperty("FILENAME").toString();
String pathToUploader =
"https://myserver.com/service/upload.php";
// Instantiate an HttpClient
HttpClient client = new HttpClient();
// Instantiate a POST HTTP method
PostMethod method = new PostMethod(pathToUploader);
method.setRequestHeader("Content-type",
exchange.getIn().getHeader("CONTENT_TYPE") + ";charset="+
exchange.getIn().getHeader("CONTENT_ENCODING"));
// Define name-value pairs to set into the QueryString
NameValuePair nvp1 = new NameValuePair("session_id", sessionId);
NameValuePair nvp2 = new NameValuePair("action", "A");
NameValuePair nvp3 = new NameValuePair("myfile", filename);
method.setQueryString(new NameValuePair[] { nvp1, nvp2, nvp3 });
try {
int statusCode = client.executeMethod(method);
System.out.println("Status Code = " + statusCode);
System.out.println("QueryString>>> " +
method.getQueryString());
System.out.println("Status Text>>>" +
HttpStatus.getStatusText(statusCode));
// Get data as a String
System.out.println(method.getResponseBodyAsString());
exchange.getIn().setHeader("HTTP_METHOD", "POST");
exchange.getIn().setBody(method.getResponseBodyAsString());
// release connection
method.releaseConnection();
} catch (IOException e) {
e.printStackTrace();
}
}
Basically I need to know how to set REST and how to set POST method for CXF
Thanks
-Br, Roman
--
View this message in context:
http://camel.465427.n5.nabble.com/Send-file-via-POST-request-tp5728674.html
Sent from the Camel - Users mailing list archive at Nabble.com.