Java Reference
In-Depth Information
The
addCancelHeader()
method creates a
Link
object for the
cancel
relationship using a
URL generated from
UriInfo.getAbsolutePathBuilder()
.
@HEAD
@Path
(
"{id}"
)
@Produces
(
"application/xml"
)
public
public
Response
getOrderHeaders
(
@PathParam
(
"id"
)
int
int
id
,
@Context
UriInfo uriInfo
)
{
Order order
=
orderDB
.
get
(
id
);
iif
(
order
==
null
null
)
{
throw
throw new
new
WebApplicationException
(
Response
.
Status
.
NOT_FOUND
);
}
Response
.
ResponseBuilder
builder
=
Response
.
ok
();
builder
.
type
(
"application/xml"
);
iif
(!
order
.
isCancelled
())
addCancelHeader
(
uriInfo
,
builder
);
return
return
builder
.
build
();
}
The
getOrderHeaders()
method processes HTTP
HEAD /orders/{id}
requests. This is a
convenience operation for HTTP clients that want the link relationships published by the re-
source but don't want to have to parse an XML document to get this information. Here, the
getOrderHeaders()
method returns the
cancel Link
header with an empty response body:
@POST
@Path
(
"{id}/cancel"
)
public
public
void
void
cancelOrder
(
@PathParam
(
"id"
)
int
int
id
)
{
Order order
=
orderDB
.
get
(
id
);
iif
(
order
==
null
null
)
{
throw
throw new
new
WebApplicationException
(
Response
.
Status
.
NOT_FOUND
);
}
order
.
setCancelled
(
true
true
);
}
Users can cancel an order by posting an empty message to
/orders/{id}/cancel
. The
can-
celOrder()
method handles these requests and simply looks up the
Order
in the database
and sets its state to cancelled.
@GET
@Produces
(
"application/xml"
)
@Formatted
public
public
Response
getOrders
(
@QueryParam
(
"start"
)
int
int
start
,