Search in sources :

Example 26 with Representation

use of org.restlet.representation.Representation in project vcell by virtualcell.

the class VCellCookieAuthenticator method login.

@Override
protected void login(Request request, Response response) {
    // Login detected
    Representation entity = request.getEntity();
    Form form = new Form(entity);
    Parameter identifier = form.getFirst(getIdentifierFormName());
    Parameter secret = form.getFirst(getSecretFormName());
    Parameter redirectURL = form.getFirst(getRedirectQueryName());
    UserLoginInfo.DigestedPassword digestedPassword = new UserLoginInfo.DigestedPassword(secret.getValue());
    try {
        User user = vcellApiApplication.getUserVerifier().authenticateUser(identifier.getValue(), digestedPassword.getString().toCharArray());
        if (user == null) {
            response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
            return;
        }
        ApiClient apiClient = vcellApiApplication.getUserVerifier().getApiClient(VCellApiApplication.BROWSER_CLIENTID);
        ApiAccessToken accessToken = vcellApiApplication.getUserVerifier().generateApiAccessToken(apiClient.getKey(), user);
        // Set credentials
        ChallengeResponse cr = new ChallengeResponse(getScheme(), CustomAuthHelper.ACCESS_TOKEN, accessToken.getToken());
        request.setChallengeResponse(cr);
        getCredentialsCookie(request, response).setMaxAge(0);
        getLogger().log(Level.INFO, "MyCookieAuthenticator.login(request,response) - created new accessToken '" + accessToken.getToken() + "' and assignd to ChallengeResponse, redirectURL='" + redirectURL.getValue() + "'");
        response.redirectSeeOther(Reference.decode(redirectURL.getValue()));
    } catch (SQLException e) {
        e.printStackTrace();
        getLogger().log(Level.SEVERE, "MyCookieAuthenticator.login(request,response) - exception", e);
    } catch (DataAccessException e) {
        e.printStackTrace();
        getLogger().log(Level.SEVERE, "MyCookieAuthenticator.login(request,response) - exception", e);
    }
}
Also used : User(org.vcell.util.document.User) Form(org.restlet.data.Form) SQLException(java.sql.SQLException) ApiAccessToken(cbit.vcell.modeldb.ApiAccessToken) Parameter(org.restlet.data.Parameter) Representation(org.restlet.representation.Representation) UserLoginInfo(org.vcell.util.document.UserLoginInfo) ApiClient(cbit.vcell.modeldb.ApiClient) DataAccessException(org.vcell.util.DataAccessException) ChallengeResponse(org.restlet.data.ChallengeResponse)

Example 27 with Representation

use of org.restlet.representation.Representation in project vcell by virtualcell.

the class PublicationsServerResource method get_html.

@Override
public Representation get_html() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    PublicationRepresentation[] publications = getPublicationRepresentations(vcellUser);
    Map<String, Object> dataModel = new HashMap<String, Object>();
    // +"?"+VCellApiApplication.REDIRECTURL_FORMNAME+"="+getRequest().getResourceRef().toUrl());
    dataModel.put("loginurl", "/" + VCellApiApplication.LOGINFORM);
    dataModel.put("logouturl", "/" + VCellApiApplication.LOGOUT + "?" + VCellApiApplication.REDIRECTURL_FORMNAME + "=" + Reference.encode(getRequest().getResourceRef().toUrl().toString()));
    if (vcellUser != null) {
        dataModel.put("userid", vcellUser.getName());
    }
    dataModel.put("pubId", getQueryValue(PARAM_PUB_ID));
    dataModel.put("orderBy", getQueryValue(PARAM_ORDERBY));
    dataModel.put("publications", Arrays.asList(publications));
    Gson gson = new Gson();
    dataModel.put("jsonResponse", gson.toJson(publications));
    Configuration templateConfiguration = application.getTemplateConfiguration();
    Representation formFtl = new ClientResource(LocalReference.createClapReference("/publications.ftl")).get();
    TemplateRepresentation templateRepresentation = new TemplateRepresentation(formFtl, templateConfiguration, dataModel, MediaType.TEXT_HTML);
    return templateRepresentation;
}
Also used : TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) Representation(org.restlet.representation.Representation) PublicationRepresentation(org.vcell.rest.common.PublicationRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource)

Example 28 with Representation

use of org.restlet.representation.Representation in project xwiki-platform by xwiki.

the class FormUrlEncodedTagsReader method readFrom.

@Override
public Tags readFrom(Class<Tags> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    ObjectFactory objectFactory = new ObjectFactory();
    Tags tags = objectFactory.createTags();
    Representation representation = new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM);
    Form form = new Form(representation);
    /*
         * If the form is empty then it might have happened that some filter has invalidated the entity stream. Try to
         * read data using getParameter()
         */
    if (form.getNames().isEmpty()) {
        HttpServletRequest httpServletRequest = ServletUtils.getRequest(Request.getCurrent());
        String text = httpServletRequest.getParameter(TAGS_FIELD_NAME);
        if (text != null) {
            String[] tagNames = text.split(" |,|\\|");
            for (String tagName : tagNames) {
                Tag tag = objectFactory.createTag();
                tag.setName(tagName);
                tags.getTags().add(tag);
            }
        }
        String[] tagNames = httpServletRequest.getParameterValues(TAG_FIELD_NAME);
        if (tagNames != null) {
            for (String tagName : tagNames) {
                Tag tag = objectFactory.createTag();
                tag.setName(tagName);
                tags.getTags().add(tag);
            }
        }
    } else {
        String text = form.getFirstValue(TAGS_FIELD_NAME);
        if (text != null) {
            String[] tagNames = text.split(" |,|\\|");
            for (String tagName : tagNames) {
                Tag tag = objectFactory.createTag();
                tag.setName(tagName);
                tags.getTags().add(tag);
            }
        }
        for (String tagName : form.getValuesArray(TAG_FIELD_NAME)) {
            Tag tag = objectFactory.createTag();
            tag.setName(tagName);
            tags.getTags().add(tag);
        }
    }
    return tags;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectFactory(org.xwiki.rest.model.jaxb.ObjectFactory) InputRepresentation(org.restlet.representation.InputRepresentation) Form(org.restlet.data.Form) InputRepresentation(org.restlet.representation.InputRepresentation) Representation(org.restlet.representation.Representation) Tag(org.xwiki.rest.model.jaxb.Tag) Tags(org.xwiki.rest.model.jaxb.Tags)

Example 29 with Representation

use of org.restlet.representation.Representation in project xwiki-platform by xwiki.

the class XWikiSetupCleanupFilter method beforeHandle.

@Override
protected int beforeHandle(Request request, Response response) {
    /*
         * We put the original HTTP request in context attributes because this is needed for reading
         * application/www-form-urlencoded POSTs. In fact servlet filters might call getParameters() which invalidates
         * the request body, making Restlet unable to process it. In this case we need to use getParameters as well
         * instead of reading form data from the input stream, and in order to do this we need the original HTTP request
         * object. This is basically a hack that should be removed as soon as the Restlet JAX-RS extension will support
         * the injection of the request object via the @Context annotation.
         */
    getContext().getAttributes().put(Constants.HTTP_REQUEST, getHttpRequest(request));
    // Workaround bug in Restlet. See https://github.com/restlet/restlet-framework-java/issues/1271.
    Representation entity = request.getEntity();
    if (entity.getMediaType() == null) {
        entity.setMediaType(MediaType.APPLICATION_OCTET_STREAM);
    }
    return Filter.CONTINUE;
}
Also used : Representation(org.restlet.representation.Representation)

Example 30 with Representation

use of org.restlet.representation.Representation in project xwiki-platform by xwiki.

the class FormUrlEncodedObjectReader method readFrom.

@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    ObjectFactory objectFactory = new ObjectFactory();
    Object object = objectFactory.createObject();
    Representation representation = new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM);
    Form form = new Form(representation);
    /*
         * If the form is empty then it might have happened that some filter has invalidated the entity stream. Try to
         * read data using getParameter()
         */
    if (form.getNames().isEmpty()) {
        HttpServletRequest httpServletRequest = ServletUtils.getRequest(Request.getCurrent());
        object.setClassName(httpServletRequest.getParameter(CLASSNAME_FIELD_NAME));
        Enumeration<String> enumeration = httpServletRequest.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement();
            if (name.startsWith(PROPERTY_PREFIX)) {
                Property property = objectFactory.createProperty();
                property.setName(name.replace(PROPERTY_PREFIX, ""));
                property.setValue(httpServletRequest.getParameter(name));
                object.getProperties().add(property);
            }
        }
    } else {
        object.setClassName(form.getFirstValue(CLASSNAME_FIELD_NAME));
        for (String name : form.getNames()) {
            if (name.startsWith(PROPERTY_PREFIX)) {
                Property property = objectFactory.createProperty();
                property.setName(name.replace(PROPERTY_PREFIX, ""));
                property.setValue(form.getFirstValue(name));
                object.getProperties().add(property);
            }
        }
    }
    return object;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectFactory(org.xwiki.rest.model.jaxb.ObjectFactory) InputRepresentation(org.restlet.representation.InputRepresentation) Form(org.restlet.data.Form) Object(org.xwiki.rest.model.jaxb.Object) InputRepresentation(org.restlet.representation.InputRepresentation) Representation(org.restlet.representation.Representation) Property(org.xwiki.rest.model.jaxb.Property)

Aggregations

Representation (org.restlet.representation.Representation)101 HashMap (java.util.HashMap)28 Test (org.testng.annotations.Test)27 StringRepresentation (org.restlet.representation.StringRepresentation)24 Request (org.restlet.Request)23 Response (org.restlet.Response)23 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)23 ResourceException (org.restlet.resource.ResourceException)21 Reference (org.restlet.data.Reference)19 StringWriter (java.io.StringWriter)17 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)16 IOException (java.io.IOException)14 Map (java.util.Map)14 Form (org.restlet.data.Form)14 VCellApiApplication (org.vcell.rest.VCellApiApplication)14 User (org.vcell.util.document.User)13 Configuration (freemarker.template.Configuration)10 StringReader (java.io.StringReader)10 ZNRecord (org.apache.helix.ZNRecord)10 TypeReference (org.codehaus.jackson.type.TypeReference)10