use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class ModelFactory method fillObjectSummary.
private void fillObjectSummary(ObjectSummary objectSummary, Document doc, BaseObject xwikiObject, Boolean withPrettyNames) {
objectSummary.setId(String.format("%s:%s", doc.getPrefixedFullName(), xwikiObject.getGuid()));
objectSummary.setGuid(xwikiObject.getGuid());
objectSummary.setPageId(doc.getPrefixedFullName());
objectSummary.setPageVersion(doc.getVersion());
objectSummary.setPageAuthor(doc.getAuthor());
if (withPrettyNames) {
XWikiContext xwikiContext = this.xcontextProvider.get();
objectSummary.setPageAuthorName(xwikiContext.getWiki().getUserName(doc.getAuthor(), null, false, xwikiContext));
}
objectSummary.setWiki(doc.getWiki());
objectSummary.setSpace(doc.getSpace());
objectSummary.setPageName(doc.getName());
objectSummary.setClassName(xwikiObject.getClassName());
objectSummary.setNumber(xwikiObject.getNumber());
String[] propertyNames = xwikiObject.getPropertyNames();
if (propertyNames.length > 0) {
try {
objectSummary.setHeadline(serializePropertyValue(xwikiObject.get(propertyNames[0])));
} catch (XWikiException e) {
// Should never happen
}
}
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class XWikiAuthentication method authenticate.
@Override
public boolean authenticate(Request request, Response response) {
/*
* Browser authentication resource is a special resource that allows to trigger the authentication dialog box in
* web browsers
*/
if (request.getResourceRef().getPath().endsWith(BrowserAuthenticationResource.URI_PATTERN)) {
return super.authenticate(request, response);
}
ComponentManager componentManager = (ComponentManager) getContext().getAttributes().get(Constants.XWIKI_COMPONENT_MANAGER);
XWikiContext xwikiContext = Utils.getXWikiContext(componentManager);
XWiki xwiki = Utils.getXWiki(componentManager);
DocumentReferenceResolver<String> resolver;
EntityReferenceSerializer<String> serializer;
try {
resolver = componentManager.getInstance(DocumentReferenceResolver.TYPE_STRING, "current");
serializer = componentManager.getInstance(EntityReferenceSerializer.TYPE_STRING);
} catch (ComponentLookupException e1) {
return false;
}
/* By default set XWiki.Guest as the user that is sending the request. */
xwikiContext.setUserReference(null);
/*
* After performing the authentication we should add headers to the response to allow applications to verify if
* the authentication is still valid We are also adding the XWiki version at the same moment.
*/
Series<Header> responseHeaders = (Series<Header>) response.getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS);
if (responseHeaders == null) {
responseHeaders = new Series<>(Header.class);
response.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, responseHeaders);
}
responseHeaders.add("XWiki-User", serializer.serialize(xwikiContext.getUserReference()));
responseHeaders.add("XWiki-Version", xwikiContext.getWiki().getVersion());
// Try with standard XWiki auth
try {
XWikiUser xwikiUser = xwiki.checkAuth(xwikiContext);
if (xwikiUser != null) {
// Make sure the user is in the context
xwikiContext.setUserReference(resolver.resolve(xwikiUser.getUser()));
getLogger().fine(String.format("Authenticated as '%s'.", xwikiUser.getUser()));
// the user has changed so we need to reset the header
responseHeaders.set("XWiki-User", serializer.serialize(xwikiContext.getUserReference()));
return true;
}
} catch (XWikiException e) {
getLogger().log(Level.WARNING, "Exception occurred while authenticating.", e);
}
// Falback on restlet auth
return super.authenticate(request, response);
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class AbstractExtensionRESTResource method resolveExtensionAuthor.
protected ExtensionAuthor resolveExtensionAuthor(String authorId) {
ExtensionAuthor author = new ExtensionAuthor();
XWikiContext xcontext = getXWikiContext();
XWikiDocument document;
try {
document = xcontext.getWiki().getDocument(authorId, xcontext);
} catch (XWikiException e) {
document = null;
}
if (document != null && !document.isNew()) {
author.setName(xcontext.getWiki().getPlainUserName(document.getDocumentReference(), xcontext));
author.setUrl(document.getExternalURL("view", xcontext));
} else {
author.setName(authorId);
}
return author;
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class AbstractExtensionRESTResource method getExtensionRating.
protected ExtensionRating getExtensionRating(DocumentReference extensionDocumentReference) {
ExtensionRating extensionRating = this.extensionObjectFactory.createExtensionRating();
try {
AverageRatingApi averageRating = new AverageRatingApi(ratingsManager.getAverageRating(extensionDocumentReference));
extensionRating.setTotalVotes(averageRating.getNbVotes());
extensionRating.setAverageVote(averageRating.getAverageVote());
} catch (XWikiException e) {
extensionRating.setTotalVotes(0);
extensionRating.setAverageVote(0);
}
return extensionRating;
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class ExtensionVersionFileRESTResource method downloadLocalExtension.
private ResponseBuilder downloadLocalExtension(String extensionId, String extensionVersion) throws ResolveException, IOException, QueryException, XWikiException {
XWikiDocument extensionDocument = getExistingExtensionDocumentById(extensionId);
checkRights(extensionDocument);
ResourceReference resourceReference = repositoryManager.getDownloadReference(extensionDocument, extensionVersion);
ResponseBuilder response = null;
if (ResourceType.ATTACHMENT.equals(resourceReference.getType())) {
// It's an attachment
AttachmentReference attachmentReference = this.attachmentResolver.resolve(resourceReference.getReference(), extensionDocument.getDocumentReference());
XWikiContext xcontext = getXWikiContext();
XWikiDocument document = xcontext.getWiki().getDocument(attachmentReference.getDocumentReference(), xcontext);
checkRights(document);
XWikiAttachment xwikiAttachment = document.getAttachment(attachmentReference.getName());
response = getAttachmentResponse(xwikiAttachment);
} else if (ResourceType.URL.equals(resourceReference.getType())) {
// It's an URL
URL url = new URL(resourceReference.getReference());
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "XWikiExtensionRepository");
httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
httpClient.setRoutePlanner(routePlanner);
HttpGet getMethod = new HttpGet(url.toString());
HttpResponse subResponse;
try {
subResponse = httpClient.execute(getMethod);
} catch (Exception e) {
throw new IOException("Failed to request [" + getMethod.getURI() + "]", e);
}
response = Response.status(subResponse.getStatusLine().getStatusCode());
// TODO: find a proper way to do a perfect proxy of the URL without directly using Restlet classes.
// Should probably use javax.ws.rs.ext.MessageBodyWriter
HttpEntity entity = subResponse.getEntity();
InputRepresentation content = new InputRepresentation(entity.getContent(), entity.getContentType() != null ? new MediaType(entity.getContentType().getValue()) : MediaType.APPLICATION_OCTET_STREAM, entity.getContentLength());
BaseObject extensionObject = getExtensionObject(extensionDocument);
String type = getValue(extensionObject, XWikiRepositoryModel.PROP_EXTENSION_TYPE);
Disposition disposition = new Disposition(Disposition.TYPE_ATTACHMENT);
disposition.setFilename(extensionId + '-' + extensionVersion + '.' + type);
content.setDisposition(disposition);
response.entity(content);
} else if (ExtensionResourceReference.TYPE.equals(resourceReference.getType())) {
ExtensionResourceReference extensionResource;
if (resourceReference instanceof ExtensionResourceReference) {
extensionResource = (ExtensionResourceReference) resourceReference;
} else {
extensionResource = new ExtensionResourceReference(resourceReference.getReference());
}
response = downloadRemoteExtension(extensionResource);
} else {
throw new WebApplicationException(Status.NOT_FOUND);
}
return response;
}
Aggregations