use of oauth2.common.ReservationConfirmation in project tesb-rt-se by Talend.
the class RestaurantReservationService method completeReservation.
@GET
@Path("complete")
@Produces({ "text/html", "application/xml;q=0.9" })
public Response completeReservation(@QueryParam("code") String code, @QueryParam("state") String state) {
String userName = sc.getUserPrincipal().getName();
Map<String, ReservationRequest> userRequests = requests.get(userName);
if (userRequests == null) {
return redirectToFailureHandler(NO_REQUEST_USER);
}
if (state == null) {
return redirectToFailureHandler(NO_REQUEST_STATE);
}
ReservationRequest request = userRequests.remove(state);
if (request == null) {
return redirectToFailureHandler(NO_REQUEST_AVAILABLE);
}
if (code == null) {
return redirectToFailureHandler(NO_CODE_GRANT);
}
LOG.info("Completing the reservation request for a user: " + request.getReserveName());
AuthorizationCodeGrant codeGrant = new AuthorizationCodeGrant(code, getCallbackURI());
LOG.info("Requesting OAuth server to replace an authorized request token with an access token");
ClientAccessToken accessToken = manager.getAccessToken(codeGrant);
if (accessToken == null) {
return redirectToFailureHandler(NO_OAUTH_ACCESS_TOKEN);
}
Calendar c = null;
try {
String authHeader = manager.createAuthorizationHeader(accessToken);
socialService.replaceHeader("Authorization", authHeader);
c = socialService.get(Calendar.class);
} catch (RuntimeException ex) {
return redirectToFailureHandler(CALENDAR_ACCESS_PROBLEM);
}
CalendarEntry entry = c.getEntry(request.getHour());
if (entry.getEventDescription() == null || entry.getEventDescription().trim().isEmpty()) {
String address = restaurantService.post(new Form().param("name", request.getReserveName()).param("phone", request.getContactPhone()).param("hour", Integer.toString(request.getHour())), String.class);
if (address == null) {
return redirectToFailureHandler(NO_RESERVATION);
}
// update the user's calendar
String authHeader = manager.createAuthorizationHeader(accessToken);
socialService.replaceHeader("Authorization", authHeader);
Response response = socialService.form(new Form().param("hour", Integer.toString(request.getHour())).param("description", "Table reserved at " + address));
boolean calendarUpdated = response.getStatus() == 200 || response.getStatus() == 204;
return Response.ok(new ReservationConfirmation(address, request.getHour(), calendarUpdated)).build();
} else {
return redirectToFailureHandler(CALENDAR_BUSY);
}
}
use of oauth2.common.ReservationConfirmation in project tesb-rt-se by Talend.
the class RESTClient method reserveTable.
public void reserveTable() throws Exception {
WebClient rs = createClient("http://localhost:" + port + "/services/reservations/reserve/table", "barry@restaurant.com", "5678");
Response r = rs.form(new Form().param("name", "Barry").param("phone", "12345678").param("hour", "7"));
int status = r.getStatus();
Object locationHeader = r.getMetadata().getFirst("Location");
if (status != 303 || locationHeader == null) {
System.out.println("OAuth flow is broken");
}
WebClient authorizeClient = createClient(locationHeader.toString(), "barry@social.com", "1234");
OAuthAuthorizationData data = authorizeClient.get(OAuthAuthorizationData.class);
Object authenticityCookie = authorizeClient.getResponse().getMetadata().getFirst("Set-Cookie");
System.out.println(authenticityCookie);
Form authorizationResult = getAuthorizationResult(data);
authorizeClient.reset();
authorizeClient.to(data.getReplyTo(), false);
if (authenticityCookie != null) {
authorizeClient.header("Cookie", (String) authenticityCookie);
}
Response r2 = authorizeClient.form(authorizationResult);
int status2 = r2.getStatus();
Object locationHeader2 = r2.getMetadata().getFirst("Location");
if (status2 != 303 || locationHeader2 == null) {
System.out.println("OAuth flow is broken");
}
WebClient finalClient = createClient(locationHeader2.toString(), "barry@restaurant.com", "5678");
finalClient.accept("application/xml");
ReservationConfirmation confirm = finalClient.get(ReservationConfirmation.class);
if (confirm != null) {
updateAndGetUserCalendar(7, "Dinner at " + confirm.getAddress());
} else {
System.out.println("Reservation failed");
}
}
Aggregations