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);
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations