Search in sources :

Example 1 with Calendar

use of oauth.common.Calendar 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("oauth_token") String token, @QueryParam("oauth_verifier") String verifier) {
    String userName = sc.getUserPrincipal().getName();
    Map<String, ReservationRequest> userRequests = requests.get(userName);
    if (userRequests == null) {
        return redirectToFailureHandler(NO_REQUEST);
    }
    ReservationRequest request = userRequests.remove(token);
    if (request == null) {
        return redirectToFailureHandler(NO_REQUEST_FOR_TOKEN);
    }
    if (verifier == null) {
        return redirectToFailureHandler(NO_VERIFIER);
    }
    LOG.info("Requesting OAuth server to replace an authorized request token with an access token");
    Token accessToken = manager.getAccessToken(request.getRequestToken(), verifier);
    if (accessToken == null) {
        return redirectToFailureHandler(NO_OAUTH_ACCESS_TOKEN);
    }
    LOG.info("Completing the reservation request for a user: " + request.getReserveName());
    Calendar c = null;
    try {
        String authHeader = manager.createAuthorizationHeader(accessToken, "GET", socialService.getCurrentURI().toString());
        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, "POST", socialService.getCurrentURI().toString());
        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);
    }
}
Also used : Response(javax.ws.rs.core.Response) CalendarEntry(oauth.common.CalendarEntry) Form(javax.ws.rs.core.Form) Calendar(oauth.common.Calendar) ReservationConfirmation(oauth.common.ReservationConfirmation) Token(org.apache.cxf.rs.security.oauth.client.OAuthClientUtils.Token) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with Calendar

use of oauth.common.Calendar in project tesb-rt-se by Talend.

the class ThirdPartyAccessService method updateCalendar.

@POST
public void updateCalendar(@FormParam("hour") int hour, @FormParam("description") String description) {
    // This permission check can be done in a custom filter; it can be simpler to do
    // in the actual service code if the context data (such as an hour in this case)
    // are not available in the request URI but in the message payload
    OAuthContext oauth = getOAuthContext();
    List<OAuthPermission> perms = oauth.getPermissions();
    boolean checkPassed = false;
    for (OAuthPermission perm : perms) {
        if (perm.getPermission().startsWith(OAuthConstants.UPDATE_CALENDAR_SCOPE)) {
            int authorizedHour = Integer.valueOf(perm.getPermission().substring(OAuthConstants.UPDATE_CALENDAR_SCOPE.length()));
            if (authorizedHour == hour) {
                checkPassed = true;
            }
        }
    }
    if (!checkPassed) {
        throw new WebApplicationException(403);
    }
    // end of the check
    Calendar calendar = getUserCalendar();
    calendar.getEntry(hour).setEventDescription(description);
}
Also used : OAuthPermission(org.apache.cxf.rs.security.oauth.data.OAuthPermission) WebApplicationException(javax.ws.rs.WebApplicationException) Calendar(oauth.common.Calendar) OAuthContext(org.apache.cxf.rs.security.oauth.data.OAuthContext) POST(javax.ws.rs.POST)

Aggregations

Calendar (oauth.common.Calendar)2 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Form (javax.ws.rs.core.Form)1 Response (javax.ws.rs.core.Response)1 CalendarEntry (oauth.common.CalendarEntry)1 ReservationConfirmation (oauth.common.ReservationConfirmation)1 Token (org.apache.cxf.rs.security.oauth.client.OAuthClientUtils.Token)1 OAuthContext (org.apache.cxf.rs.security.oauth.data.OAuthContext)1 OAuthPermission (org.apache.cxf.rs.security.oauth.data.OAuthPermission)1