Search in sources :

Example 16 with Post

use of org.restlet.resource.Post in project OpenAM by OpenRock.

the class AuthorizeResource method authorize.

/**
     * Handles POST requests to the OAuth2 authorize endpoint.
     * <br/>
     * This method will be called when a user has given their consent for an authorization request.
     *
     * @param entity The entity on the request.
     * @return The body to be sent in the response to the user agent.
     * @throws OAuth2RestletException If a OAuth2 error occurs whilst processing the authorization request.
     */
@Post
public Representation authorize(Representation entity) throws OAuth2RestletException {
    final OAuth2Request request = requestFactory.create(getRequest());
    for (AuthorizeRequestHook hook : hooks) {
        hook.beforeAuthorizeHandling(request, getRequest(), getResponse());
    }
    final boolean consentGiven = "allow".equalsIgnoreCase(request.<String>getParameter("decision"));
    final boolean saveConsent = "on".equalsIgnoreCase(request.<String>getParameter("save_consent"));
    try {
        final AuthorizationToken authorizationToken = authorizationService.authorize(request, consentGiven, saveConsent);
        final String redirectUri = request.getParameter("redirect_uri");
        Representation response = representation.toRepresentation(getContext(), getRequest(), getResponse(), authorizationToken, redirectUri);
        for (AuthorizeRequestHook hook : hooks) {
            hook.afterAuthorizeSuccess(request, getRequest(), getResponse());
        }
        return response;
    } catch (ResourceOwnerAuthenticationRequired e) {
        throw new OAuth2RestletException(e.getStatusCode(), e.getError(), e.getMessage(), e.getRedirectUri().toString(), null);
    } catch (InvalidClientException e) {
        throw new OAuth2RestletException(e.getStatusCode(), e.getError(), e.getMessage(), request.<String>getParameter("state"));
    } catch (RedirectUriMismatchException e) {
        throw new OAuth2RestletException(e.getStatusCode(), e.getError(), e.getMessage(), request.<String>getParameter("state"));
    } catch (OAuth2Exception e) {
        throw new OAuth2RestletException(e.getStatusCode(), e.getError(), e.getMessage(), request.<String>getParameter("redirect_uri"), request.<String>getParameter("state"), e.getParameterLocation());
    }
}
Also used : ResourceOwnerAuthenticationRequired(org.forgerock.oauth2.core.exceptions.ResourceOwnerAuthenticationRequired) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) AuthorizationToken(org.forgerock.oauth2.core.AuthorizationToken) RedirectUriMismatchException(org.forgerock.oauth2.core.exceptions.RedirectUriMismatchException) InvalidClientException(org.forgerock.oauth2.core.exceptions.InvalidClientException) Representation(org.restlet.representation.Representation) OAuth2Exception(org.forgerock.oauth2.core.exceptions.OAuth2Exception) Post(org.restlet.resource.Post)

Example 17 with Post

use of org.restlet.resource.Post in project netxms by netxms.

the class AbstractHandler method onPost.

/**
 * Process POST requests
 *
 * @param entity
 * @return
 */
@Post
public Representation onPost(Representation entity) throws Exception {
    if (entity == null) {
        log.warn("No POST data in call");
        return new StringRepresentation(createErrorResponse(RCC.ACCESS_DENIED).toString(), MediaType.APPLICATION_JSON);
    }
    JSONObject data = new JsonRepresentation(entity).getJsonObject();
    log.debug("POST: data = " + data);
    if (attachToSession()) {
        return new StringRepresentation(JsonTools.jsonFromObject(create(data), null), MediaType.APPLICATION_JSON);
    } else {
        return new StringRepresentation(createErrorResponse(RCC.ACCESS_DENIED).toString(), MediaType.APPLICATION_JSON);
    }
}
Also used : JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Post(org.restlet.resource.Post)

Example 18 with Post

use of org.restlet.resource.Post in project netxms by netxms.

the class AccessIntegrationTools method onPost.

/* (non-Javadoc)
    * @see org.netxms.websvc.handlers.AbstractHandler#onPost(org.restlet.representation.Representation)
    */
@Override
@Post
public Representation onPost(Representation entity) throws Exception {
    int rc = RCC.ACCESS_DENIED;
    if (entity != null) {
        JSONObject request = new JsonRepresentation(entity).getJsonObject();
        String login = request.getString("login");
        String password = request.getString("password");
        if ((login != null) || (password != null)) {
            NXCSession session = new NXCSession(properties.getServerAddress(), properties.getServerPort());
            session.connect();
            session.login(login, (password == null) ? "" : password);
            if ((session.getUserSystemRights() & UserAccessRights.SYSTEM_ACCESS_EXTERNAL_INTEGRATION) != 0)
                rc = RCC.SUCCESS;
            session.disconnect();
        } else {
            log.debug("Login or password not specified in login call");
            rc = RCC.INVALID_REQUEST;
        }
    } else {
        log.debug("No POST data in login call");
        rc = RCC.INVALID_REQUEST;
    }
    return new StringRepresentation(createErrorResponse(rc).toString(), MediaType.APPLICATION_JSON);
}
Also used : NXCSession(org.netxms.client.NXCSession) JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Post(org.restlet.resource.Post)

Example 19 with Post

use of org.restlet.resource.Post in project vcell by virtualcell.

the class OptimizationRunServerResource method run.

@Override
@Post
public Representation run(Representation optProblemJson) {
    OptSocketStreams optSocketStreams = (OptSocketStreams) doOP(optProblemJson, this);
    getResponse().setStatus(Status.SUCCESS_OK);
    Representation representation = new StringRepresentation(optSocketStreams.optID, MediaType.TEXT_PLAIN);
    return representation;
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) Post(org.restlet.resource.Post)

Example 20 with Post

use of org.restlet.resource.Post in project vcell by virtualcell.

the class PublicationServerResource method handleForm.

@Post
public Representation handleForm(Representation entity) {
    try {
        String myHost = getHostRef().getHostDomain();
        String allowedPublicationHost = PropertyLoader.getProperty(PropertyLoader.vcellPublicationHost, "");
        if (!allowedPublicationHost.equals(myHost)) {
            throw new PermissionException("Host '" + myHost + "' not allowed to edit publications");
        }
        Form form = new Form(entity);
        VCellApiApplication application = ((VCellApiApplication) getApplication());
        User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
        if (vcellUser == null) {
            throw new PermissionException("must be authenticated to edit publication info");
        }
        // The form contains input with names "user" and "password"
        String pubop = form.getFirstValue("pubop");
        // String password = form.getFirstValue("password");
        Map<String, Object> dataModel = new HashMap<String, Object>();
        if (pubop == null) {
            return new StringRepresentation(("value of publication 'operation' cannot be null"));
        } else if (pubop.equals("editNew")) {
            PublicationRepresentation value = new PublicationRepresentation();
            value.pubKey = AUTOMATICALLY_GENERATED;
            dataModel.put("publicationRepr", value);
        } else if (pubop.equals("editWithKey")) {
            Object pubidObj = getRequestAttributes().get(VCellApiApplication.PUBLICATIONID);
            PublicationRepresentation publication = getPublicationRepresentation(((VCellApiApplication) getApplication()).getRestDatabaseService(), vcellUser, new KeyValue(pubidObj.toString()));
            dataModel.put("publicationRepr", publication);
        } else if (pubop.equals("applyEdit")) {
            SimpleDateFormat sdf = new java.text.SimpleDateFormat("MM/dd/yyyy", java.util.Locale.US);
            // PublicationRepresentation publication = getPublicationRepresentation(((VCellApiApplication)getApplication()).getRestDatabaseService(),vcellUser,new KeyValue(form.getFirstValue("pubid")));
            String[] authors = StringUtils.split(form.getFirstValue("authors"), ";");
            for (int i = 0; i < authors.length; i++) {
                authors[i] = authors[i].trim();
            }
            String thePubID = form.getFirstValue("pubId");
            BioModelReferenceRep[] bioModelReferenceReps = (BioModelReferenceRep[]) convertToReferenceRep(form.getFirstValue("biomodelReferences"), BioModelReferenceRep.class);
            MathModelReferenceRep[] mathModelReferenceReps = (MathModelReferenceRep[]) convertToReferenceRep(form.getFirstValue("mathmodelReferences"), MathModelReferenceRep.class);
            PublicationRep publicationRep = new PublicationRep((thePubID == null || thePubID.equals(AUTOMATICALLY_GENERATED) ? null : new KeyValue(Integer.valueOf(thePubID).toString())), form.getFirstValue("title"), authors, (form.getFirstValue("year") == null || form.getFirstValue("year").trim().length() == 0 ? null : Integer.valueOf(form.getFirstValue("year"))), form.getFirstValue("citation"), form.getFirstValue("pubmedid"), form.getFirstValue("doi"), form.getFirstValue("endnoteid"), form.getFirstValue("url"), bioModelReferenceReps, mathModelReferenceReps, form.getFirstValue("wittid"), (form.getFirstValue("pubdate") == null || form.getFirstValue("pubdate").trim().length() == 0 ? null : sdf.parse(form.getFirstValue("pubdate"))));
            KeyValue savedOrEditedPubID = ((VCellApiApplication) getApplication()).getRestDatabaseService().savePublicationRep(publicationRep, vcellUser);
            // String address = getRequest().getClientInfo().getAddress();
            // int port = getRequest().getClientInfo().getPort();
            StringRepresentation s = getPubInfoHtml(myHost, savedOrEditedPubID);
            return s;
        } else if (pubop.equals("makepublic")) {
            String[] bmKeys = form.getValuesArray("bmpublic");
            KeyValue[] publishTheseBiomodels = new KeyValue[(bmKeys == null ? 0 : bmKeys.length)];
            for (int i = 0; i < publishTheseBiomodels.length; i++) {
                publishTheseBiomodels[i] = new KeyValue(bmKeys[i]);
            }
            String[] mmKeys = form.getValuesArray("mmpublic");
            KeyValue[] publishTheseMathmodels = new KeyValue[(mmKeys == null ? 0 : mmKeys.length)];
            for (int i = 0; i < publishTheseMathmodels.length; i++) {
                publishTheseMathmodels[i] = new KeyValue(mmKeys[i]);
            }
            if (publishTheseBiomodels.length > 0 || publishTheseMathmodels.length > 0) {
                ((VCellApiApplication) getApplication()).getRestDatabaseService().publishDirectly(publishTheseBiomodels, publishTheseMathmodels, vcellUser);
            }
            Object pubidObj = getRequestAttributes().get(VCellApiApplication.PUBLICATIONID);
            StringRepresentation s = getPubInfoHtml(myHost, new KeyValue(pubidObj.toString()));
            return s;
        } else {
            return new StringRepresentation(("value of pubop=" + pubop + " not expected").toCharArray());
        }
        Configuration templateConfiguration = application.getTemplateConfiguration();
        Representation myRepresentation = new ClientResource(LocalReference.createClapReference("/newpublication.ftl")).get();
        TemplateRepresentation templateRepresentation = new TemplateRepresentation(myRepresentation, templateConfiguration, dataModel, MediaType.TEXT_HTML);
        return templateRepresentation;
    } catch (Exception e) {
        return new StringRepresentation(e.getClass().getName() + " " + e.getMessage());
    }
}
Also used : PermissionException(org.vcell.util.PermissionException) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) KeyValue(org.vcell.util.document.KeyValue) Configuration(freemarker.template.Configuration) Form(org.restlet.data.Form) HashMap(java.util.HashMap) SimpleDateFormat(java.text.SimpleDateFormat) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) BioModelReferenceRep(cbit.vcell.modeldb.BioModelReferenceRep) PublicationRep(cbit.vcell.modeldb.PublicationRep) StringRepresentation(org.restlet.representation.StringRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource) MathModelReferenceRep(cbit.vcell.modeldb.MathModelReferenceRep) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) MathmodelReferenceRepresentation(org.vcell.rest.common.MathmodelReferenceRepresentation) BiomodelReferenceRepresentation(org.vcell.rest.common.BiomodelReferenceRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) Representation(org.restlet.representation.Representation) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) ResourceException(org.restlet.resource.ResourceException) SimpleDateFormat(java.text.SimpleDateFormat) Post(org.restlet.resource.Post)

Aggregations

Post (org.restlet.resource.Post)20 StringRepresentation (org.restlet.representation.StringRepresentation)9 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)7 IOException (java.io.IOException)5 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)5 HashMap (java.util.HashMap)4 JsonValue (org.forgerock.json.JsonValue)4 OAuth2Exception (org.forgerock.oauth2.core.exceptions.OAuth2Exception)4 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 Representation (org.restlet.representation.Representation)4 OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)3 Request (org.restlet.Request)3 DeviceCode (org.forgerock.oauth2.core.DeviceCode)2 InvalidClientException (org.forgerock.oauth2.core.exceptions.InvalidClientException)2 RedirectUriMismatchException (org.forgerock.oauth2.core.exceptions.RedirectUriMismatchException)2 ResourceOwnerAuthenticationRequired (org.forgerock.oauth2.core.exceptions.ResourceOwnerAuthenticationRequired)2 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)2 ISwitchManager (org.openkilda.floodlight.switchmanager.ISwitchManager)2 MessageError (org.openkilda.messaging.error.MessageError)2