Search in sources :

Example 11 with XWikiURLFactory

use of com.xpn.xwiki.web.XWikiURLFactory in project xwiki-platform by xwiki.

the class XWikiMockitoTest method getURLWithDotsAndBackslashInSpaceName.

@Test
public void getURLWithDotsAndBackslashInSpaceName() throws Exception {
    XWikiURLFactory urlFactory = mock(XWikiURLFactory.class);
    context.setURLFactory(urlFactory);
    DocumentReference reference = new DocumentReference("wiki", Arrays.asList("space.withdot.and\\and:"), "page");
    this.xwiki.getURL(reference, "view", null, null, context);
    verify(urlFactory).createURL("space\\.withdot\\.and\\\\and\\:", "page", "view", null, null, "wiki", context);
}
Also used : XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 12 with XWikiURLFactory

use of com.xpn.xwiki.web.XWikiURLFactory in project xwiki-platform by xwiki.

the class XWikiMockitoTest method getURLWithLocale.

@Test
public void getURLWithLocale() throws Exception {
    XWikiURLFactory urlFactory = mock(XWikiURLFactory.class);
    context.setURLFactory(urlFactory);
    DocumentReference reference = new DocumentReference("wiki", "Space", "Page", Locale.FRENCH);
    this.xwiki.getURL(reference, "view", null, null, context);
    verify(urlFactory).createURL("Space", "Page", "view", "language=fr", null, "wiki", context);
    this.xwiki.getURL(reference, "view", "language=ro", null, context);
    verify(urlFactory).createURL("Space", "Page", "view", "language=ro&language=fr", null, "wiki", context);
}
Also used : XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 13 with XWikiURLFactory

use of com.xpn.xwiki.web.XWikiURLFactory in project xwiki-platform by xwiki.

the class XWikiMockitoTest method getEntityURLWithDefaultAction.

@Test
public void getEntityURLWithDefaultAction() throws Exception {
    DocumentReference documentReference = new DocumentReference("tennis", Arrays.asList("Path", "To"), "Success");
    AttachmentReference attachmentReference = new AttachmentReference("image.png", documentReference);
    XWikiURLFactory urlFactory = mock(XWikiURLFactory.class);
    context.setURLFactory(urlFactory);
    this.xwiki.getURL(documentReference, this.context);
    verify(urlFactory).createURL("Path.To", "Success", "view", null, null, "tennis", this.context);
    this.xwiki.getURL(attachmentReference, this.context);
    verify(urlFactory).createAttachmentURL("image.png", "Path.To", "Success", "download", null, "tennis", this.context);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 14 with XWikiURLFactory

use of com.xpn.xwiki.web.XWikiURLFactory in project xwiki-platform by xwiki.

the class AbstractAnnotationRESTResource method renderDocumentWithAnnotations.

/**
 * Helper function to get the rendered content of the document with annotations. All setup of context for rendering
 * content similar to the rendering on standard view will be done in this function. <br>
 * FIXME: find out if this whole context setup code has to be here or in the annotations service
 *
 * @param docName the name of the document to render
 * @param language the language in which to render the document
 * @param action the context action to render the document for
 * @param annotations the annotations to render on the document
 * @return the HTML rendered content of the document
 * @throws XWikiException if anything wrong happens while setting up the context for rendering
 * @throws AnnotationServiceException if anything goes wrong during the rendering of the annotations
 */
private String renderDocumentWithAnnotations(String docName, String language, String action, Collection<Annotation> annotations) throws XWikiException, AnnotationServiceException {
    String isInRenderingEngineKey = "isInRenderingEngine";
    XWikiContext context = this.xcontextProvider.get();
    Object isInRenderingEngine = context.get(isInRenderingEngineKey);
    // set the context url factory to the servlet url factory so that all links get correctly generated as if we
    // were view-ing the page
    XWikiURLFactory oldFactory = context.getURLFactory();
    int oldMode = context.getMode();
    String result = null;
    try {
        context.setMode(XWikiContext.MODE_SERVLET);
        XWikiURLFactory urlf = context.getWiki().getURLFactoryService().createURLFactory(context.getMode(), context);
        context.setURLFactory(urlf);
        // setup documents on the context, and velocity context, and message tool for i18n and all
        setUpDocuments(docName, language);
        // set the current action on the context
        context.setAction(action);
        context.put(isInRenderingEngineKey, true);
        // render the content in xhtml syntax, with the passed list of annotations
        result = annotationService.getAnnotatedRenderedContent(docName, null, "xhtml/1.0", annotations);
    } finally {
        if (isInRenderingEngine != null) {
            context.put(isInRenderingEngineKey, isInRenderingEngine);
        } else {
            context.remove(isInRenderingEngineKey);
        }
        context.setURLFactory(oldFactory);
        context.setMode(oldMode);
    }
    return result;
}
Also used : XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 15 with XWikiURLFactory

use of com.xpn.xwiki.web.XWikiURLFactory in project xwiki-platform by xwiki.

the class DefaultMailTemplateManager method evaluate.

@Override
public String evaluate(DocumentReference templateReference, String property, Map<String, Object> velocityVariables, Object localeValue) throws MessagingException {
    Locale locale = getLocale(localeValue);
    // Note: Make sure to use the class reference relative to the template's wiki and not the current wiki.
    DocumentReference mailClassReference = this.resolver.resolve(MAIL_CLASS, templateReference.getWikiReference());
    VelocityContext velocityContext = createVelocityContext(velocityVariables);
    String templateFullName = this.serializer.serialize(templateReference);
    int objectNumber = getObjectMailNumber(templateReference, mailClassReference, locale);
    String content = this.documentBridge.getProperty(templateReference, mailClassReference, objectNumber, property).toString();
    // Save the current URL Factory since we'll replace it with a URL factory that generates external URLs (ie
    // full URLs).
    XWikiContext xcontext = this.xwikiContextProvider.get();
    XWikiURLFactory originalURLFactory = xcontext.getURLFactory();
    Locale originalLocale = xcontext.getLocale();
    try {
        // Generate full URLs (instead of relative URLs)
        xcontext.setURLFactory(new ExternalServletURLFactory(xcontext));
        // Set the current locale to be the passed locale so that the template content is evaluated in that
        // language (in case there are translations used).
        xcontext.setLocale(locale);
        return velocityEvaluator.evaluateVelocity(content, templateFullName, velocityContext);
    } catch (XWikiException e) {
        throw new MessagingException(String.format("Failed to evaluate property [%s] for Document [%s] and locale [%s]", property, templateReference, localeValue), e);
    } finally {
        xcontext.setURLFactory(originalURLFactory);
        xcontext.setLocale(originalLocale);
    }
}
Also used : Locale(java.util.Locale) XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) ExternalServletURLFactory(com.xpn.xwiki.web.ExternalServletURLFactory) MessagingException(javax.mail.MessagingException) VelocityContext(org.apache.velocity.VelocityContext) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

XWikiURLFactory (com.xpn.xwiki.web.XWikiURLFactory)16 XWikiContext (com.xpn.xwiki.XWikiContext)10 URL (java.net.URL)6 DocumentReference (org.xwiki.model.reference.DocumentReference)6 Test (org.junit.Test)4 ExternalServletURLFactory (com.xpn.xwiki.web.ExternalServletURLFactory)3 XWikiServletResponseStub (com.xpn.xwiki.web.XWikiServletResponseStub)3 XWiki (com.xpn.xwiki.XWiki)2 XWikiException (com.xpn.xwiki.XWikiException)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2 IOException (java.io.IOException)2 Locale (java.util.Locale)2 MessagingException (javax.mail.MessagingException)2 VelocityContext (org.apache.velocity.VelocityContext)2 IBlogServiceRole (com.celements.blog.service.IBlogServiceRole)1 Document (com.xpn.xwiki.api.Document)1 WikiSkin (com.xpn.xwiki.internal.skin.WikiSkin)1 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)1 XWikiRequest (com.xpn.xwiki.web.XWikiRequest)1