Surjendu,
I send you a simple Client and Server class both coded with the Restlet
framework.
The client sends a file to the server providing a login/password.
The server prints login, password and file content.
I hope it will help you.
regards,
Thierry Boileau
This is my client call using Apache HttpClient Library
public static void addAttachment(String pageId) throws Exception {
HttpClient client = new HttpClient();
MultipartPostMethod mPost = new
MultipartPostMethod(getPageUri(pageId));
client.setConnectionTimeout(8000);
File f = new File("C:\\input.txt");
System.out.println("File Length" + f.length());
mPost.addParameter(f.getName(), f);
Credentials defaultcreds = new
UsernamePasswordCredentials("xxx",
"xxx");
client.getState().setCredentials(
new AuthScope("host", port,
AuthScope.ANY_REALM),
defaultcreds);
mPost.setDoAuthentication(true);
client.executeMethod(mPost);
String response = mPost.getResponseBodyAsString();
System.out.println(response);
mPost.releaseConnection();
}
In the server the following lines give null:
String identifier = request.getChallengeResponse().getIdentifier();
String credentials =
request.getChallengeResponse().getCredentials();
Why is this so? Why no identifier and credentials.
import org.restlet.Client;
import org.restlet.data.ChallengeResponse;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Protocol;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.FileRepresentation;
import org.restlet.resource.Representation;
public class SendFileClient {
public static void main(String[] args) {
Client client = new Client(Protocol.HTTP);
Request request = new Request(Method.POST, "http://localhost:8182/");
Representation representation = new FileRepresentation(
"d:\\temp\\essai.txt", MediaType.TEXT_PLAIN);
request.setEntity(representation);
ChallengeResponse challengeResponse = new ChallengeResponse(
ChallengeScheme.HTTP_BASIC, "login", "password");
request.setChallengeResponse(challengeResponse);
Response response = client.handle(request);
System.out.println(response.getStatus());
}
}
import java.io.IOException;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.Protocol;
import org.restlet.data.Request;
import org.restlet.data.Response;
public class SendFileServer {
public static void main(String[] args) throws Exception {
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8182);
Restlet restlet = new Restlet(component.getContext()) {
@Override
public void handle(Request request, Response response) {
// Prints the login
System.out.println(request.getChallengeResponse()
.getIdentifier());
// Prints the password
System.out.println(request.getChallengeResponse()
.getSecret());
try {
request.getEntity().write(System.out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
component.getDefaultHost().attachDefault(restlet);
component.start();
}
}