use of org.restlet.Request in project OpenAM by OpenRock.
the class ClientCredentialsReader method extractCredentials.
/**
* Extracts the client's credentials from the OAuth2 request.
*
* @param request The OAuth2 request.
* @param endpoint The endpoint this request should be for, or null to disable audience verification.
* @return The client's credentials.
* @throws InvalidRequestException If the request contains multiple client credentials.
* @throws InvalidClientException If the request does not contain the client's id.
*/
public ClientCredentials extractCredentials(OAuth2Request request, String endpoint) throws InvalidRequestException, InvalidClientException, NotFoundException {
final Request req = request.getRequest();
boolean basicAuth = false;
if (req.getChallengeResponse() != null) {
basicAuth = true;
}
final ClientCredentials client;
Client.TokenEndpointAuthMethod method = CLIENT_SECRET_POST;
//jwt type first
if (JWT_PROFILE_CLIENT_ASSERTION_TYPE.equalsIgnoreCase(request.<String>getParameter(CLIENT_ASSERTION_TYPE))) {
client = verifyJwtBearer(request, basicAuth, endpoint);
method = PRIVATE_KEY_JWT;
} else {
String clientId = request.getParameter(OAuth2Constants.Params.CLIENT_ID);
String clientSecret = request.getParameter(OAuth2Constants.Params.CLIENT_SECRET);
if (basicAuth && clientId != null) {
logger.error("Client (" + clientId + ") using multiple authentication methods");
throw new InvalidRequestException("Client authentication failed");
}
if (req.getChallengeResponse() != null) {
final ChallengeResponse challengeResponse = req.getChallengeResponse();
clientId = challengeResponse.getIdentifier();
clientSecret = "";
if (challengeResponse.getSecret() != null && challengeResponse.getSecret().length > 0) {
clientSecret = String.valueOf(req.getChallengeResponse().getSecret());
}
method = CLIENT_SECRET_BASIC;
}
if (clientId == null || clientId.isEmpty()) {
logger.error("Client Id is not set");
throw failureFactory.getException(request, "Client authentication failed");
}
client = new ClientCredentials(clientId, clientSecret == null ? null : clientSecret.toCharArray(), false, basicAuth);
}
final OpenIdConnectClientRegistration cr = clientRegistrationStore.get(client.getClientId(), request);
final Set<String> scopes = cr.getAllowedScopes();
//if we're accessing the token endpoint, check we're authenticating using the appropriate method
if (scopes.contains(OAuth2Constants.Params.OPENID) && req.getResourceRef().getLastSegment().equals(OAuth2Constants.Params.ACCESS_TOKEN) && !cr.getTokenEndpointAuthMethod().equals(method.getType())) {
throw failureFactory.getException(request, "Invalid authentication method for accessing this endpoint.");
}
return client;
}
use of org.restlet.Request in project lucene-solr by apache.
the class TestRestManager method testResolveResourceId.
@Test
public void testResolveResourceId() throws Exception {
Request testRequest = new Request();
Reference rootRef = new Reference("http://solr.apache.org/");
testRequest.setRootRef(rootRef);
Reference resourceRef = new Reference("http://solr.apache.org/schema/analysis/synonyms/de");
testRequest.setResourceRef(resourceRef);
String resourceId = RestManager.ManagedEndpoint.resolveResourceId(testRequest);
assertEquals(resourceId, "/schema/analysis/synonyms/de");
}
use of org.restlet.Request in project camel by apache.
the class RestletConsumer method createRestlet.
protected Restlet createRestlet() {
return new Restlet() {
@Override
public void handle(Request request, Response response) {
// must call super according to restlet documentation
super.handle(request, response);
LOG.debug("Consumer restlet handle request method: {}", request.getMethod());
Exchange exchange = null;
try {
// we want to handle the UoW
exchange = getEndpoint().createExchange();
createUoW(exchange);
RestletBinding binding = getEndpoint().getRestletBinding();
binding.populateExchangeFromRestletRequest(request, response, exchange);
try {
getProcessor().process(exchange);
} catch (Exception e) {
exchange.setException(e);
}
binding.populateRestletResponseFromExchange(exchange, response);
// resetlet will call the callback when its done sending where it would be safe
// to call doneUoW
Uniform callback = newResponseUniform(exchange);
response.setOnError(callback);
response.setOnSent(callback);
} catch (Throwable e) {
getExceptionHandler().handleException("Error processing request", exchange, e);
if (exchange != null) {
doneUoW(exchange);
}
}
}
};
}
use of org.restlet.Request in project camel by apache.
the class RestletProducer method process.
@Override
public boolean process(final Exchange exchange, final AsyncCallback callback) {
RestletEndpoint endpoint = (RestletEndpoint) getEndpoint();
// force processing synchronously using different api
if (endpoint.isSynchronous()) {
try {
process(exchange);
} catch (Throwable e) {
exchange.setException(e);
}
callback.done(true);
return true;
}
LOG.trace("Processing asynchronously");
final RestletBinding binding = endpoint.getRestletBinding();
Request request;
try {
String resourceUri = buildUri(endpoint, exchange);
URI uri = new URI(resourceUri);
request = new Request(endpoint.getRestletMethod(), resourceUri);
binding.populateRestletRequestFromExchange(request, exchange);
loadCookies(exchange, uri, request);
} catch (Throwable e) {
// break out in case of exception
exchange.setException(e);
callback.done(true);
return true;
}
// process the request asynchronously
LOG.debug("Sending request asynchronously: {} for exchangeId: {}", request, exchange.getExchangeId());
client.handle(request, new Uniform() {
@Override
public void handle(Request request, Response response) {
LOG.debug("Received response asynchronously: {} for exchangeId: {}", response, exchange.getExchangeId());
try {
if (response != null) {
String resourceUri = buildUri(endpoint, exchange);
URI uri = new URI(resourceUri);
Integer respCode = response.getStatus().getCode();
storeCookies(exchange, uri, response);
if (respCode > 207 && throwException) {
exchange.setException(populateRestletProducerException(exchange, response, respCode));
} else {
binding.populateExchangeFromRestletResponse(exchange, response);
}
}
} catch (Throwable e) {
exchange.setException(e);
} finally {
callback.done(false);
}
}
});
// we continue routing async
return false;
}
use of org.restlet.Request in project camel by apache.
the class RestletRequestAndResponseAPITest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").to("restlet:http://localhost:" + portNum + "/users/{id}/like/{beverage.beer}");
// START SNIPPET: e1
from("restlet:http://localhost:" + portNum + "/users/{id}/like/{beer}").process(new Processor() {
public void process(Exchange exchange) throws Exception {
// the Restlet request should be available if needed
Request request = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, Request.class);
assertNotNull("Restlet Request", request);
// use Restlet API to create the response
Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
assertNotNull("Restlet Response", response);
response.setStatus(Status.SUCCESS_OK);
response.setEntity("<response>Beer is Good</response>", MediaType.TEXT_XML);
exchange.getOut().setBody(response);
}
});
// END SNIPPET: e1
}
};
}
Aggregations