Is there a way to send a client request inside a HTTP method?
For example, I have Items resource and it has POST method
@Post
public Representation acceptItem(Representation entity) {
1. get information from entity
2. create new Item
3. setStatus(Status.SUCCESS_CREATED)
4. send POST request of another resource (e.g. orders resource)
ClientResource ordersResource = new ClientResource(ordersURI);
orderResource.post(info);
}
code:
public class ItemsResource extends BaseResource {
/**
* Handle POST requests: create a new item.
*/
@Post
public Representation acceptItem(Representation entity) {
Representation result = null;
// Parse the given representation and retrieve pairs of
// "name=value" tokens.
Form form = new Form(entity);
String itemName = form.getFirstValue("name");
String itemDescription = form.getFirstValue("description");
// Register the new item if one is not already registered.
if (!getItems().containsKey(itemName)
&& getItems().putIfAbsent(itemName,
new Item(itemName, itemDescription)) == null) {
// Set the response's status and entity
setStatus(Status.SUCCESS_CREATED);
Representation rep = new StringRepresentation("Item created",
MediaType.TEXT_PLAIN);
// Indicates where is located the new resource.
rep.setLocationRef(getRequest().getResourceRef().getIdentifier() + "/"
+ itemName);
result = rep;
//**In this POST method, send another POST request on another resource**
ClientResource ordersResource = new ClientResource(
"http://localhost:8111/firstResource/orders");
ClientResource orderResource = null;
// Create a new item
Order order = new Order("order1", "this is an order.");
try {
Representation rO = ordersResource.post(getRepresentation(order));
orderResource = new ClientResource(rO.getLocationRef());
} catch (ResourceException e) {
System.out.println("Error status: " + e.getStatus());
System.out.println("Error message: " + e.getMessage());
}
} else { // Item is already registered.
setStatus(Status.CLIENT_ERROR_NOT_FOUND);
result = generateErrorRepresentation("Item " + itemName
+ " already exists.", "1");
}
return result;
}
}
So in this way, when the client send a POST request on items resource, not only he creates an Item, but also creates an order.
I tried it in my code but it caused error:
Sep 16, 2014 6:57:49 PM org.restlet.engine.component.ClientRouter getNext
WARNING: The protocol used by this request is not declared in the list of client connectors. (HTTP/1.1). In case you are using an instance of the Component class, check its "clients" property.
Not Found (404) - The server has not found anything matching the request URI
If I put these two lines( ClientResource ordersResource = new ClientResource(ordersURI); orderResource.post(info);
) in the main method, it works. If I put them in a HTTP request, it doesn't work.
So I'm looking for a way to send a client request in a HTTP method?
You need to add the below init params inside the servlet declation in the web.xml . With this your application will be able to make HTTP HTTPS and FILE protocol calls.
<init-param>
<param-name>org.restlet.clients</param-name>
<param-value>HTTP HTTPS FILE</param-value>
</init-param>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.