Search in sources :

Example 1 with ConversionException

use of org.xwiki.properties.converter.ConversionException in project xwiki-platform by xwiki.

the class RecipientConverter method convertToType.

@Override
protected <G extends Message.RecipientType> G convertToType(Type targetType, Object value) {
    if (value == null) {
        return null;
    }
    Message.RecipientType recipientType;
    String valueAsString = value.toString();
    if (valueAsString.equalsIgnoreCase("to")) {
        recipientType = Message.RecipientType.TO;
    } else if (valueAsString.equalsIgnoreCase("cc")) {
        recipientType = Message.RecipientType.CC;
    } else if (valueAsString.equalsIgnoreCase("bcc")) {
        recipientType = Message.RecipientType.BCC;
    } else if (valueAsString.equalsIgnoreCase("newsgroups")) {
        recipientType = MimeMessage.RecipientType.NEWSGROUPS;
    } else {
        throw new ConversionException(String.format("Cannot convert [%s] to [%s]", value, Message.RecipientType.class.getName()));
    }
    return (G) recipientType;
}
Also used : ConversionException(org.xwiki.properties.converter.ConversionException) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage)

Example 2 with ConversionException

use of org.xwiki.properties.converter.ConversionException in project xwiki-platform by xwiki.

the class DefaultWikiComponentMethodExecutor method castRenderedContent.

/**
 * Render a XDOM and return a value converted from the rendered content. The type matches the return value of the
 * passed method.
 *
 * @param xdom The XDOM to render
 * @param method The method called
 * @return A value matching the method return type
 * @throws WikiComponentRuntimeException When the conversion fails
 */
private Object castRenderedContent(XDOM xdom, Method method) throws WikiComponentRuntimeException {
    // Since no return value has been explicitly provided, we try to convert the result of the rendering
    // into the expected return type using a Converter.
    WikiPrinter printer = new DefaultWikiPrinter();
    blockRenderer.render(xdom, printer);
    String contentResult = printer.toString();
    // Do the conversion!
    try {
        return converterManager.convert(method.getGenericReturnType(), contentResult);
    } catch (ConversionException e) {
        // Surrender!
        throw new WikiComponentRuntimeException(String.format("Failed to convert result [%s] to type [%s] for method [%s.%s]", contentResult, method.getGenericReturnType(), method.getDeclaringClass().getName(), method.getName()), e);
    }
}
Also used : ConversionException(org.xwiki.properties.converter.ConversionException) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiComponentRuntimeException(org.xwiki.component.wiki.WikiComponentRuntimeException)

Example 3 with ConversionException

use of org.xwiki.properties.converter.ConversionException in project xwiki-platform by xwiki.

the class PathConverterTest method convertWhenError.

@Test
public void convertWhenError() throws Exception {
    ResourceReferenceSerializer<VfsResourceReference, URI> serializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, URI.class), "truevfs");
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:Sandbox.WebHome@my.zip"), "a/b/c");
    when(serializer.serialize(reference)).thenThrow(new SerializeResourceReferenceException("error"));
    try {
        this.mocker.getComponentUnderTest().convert(new DefaultParameterizedType(null, Path.class), "attach:Sandbox.WebHome@my.zip/a/b/c");
        fail("Should have thrown an exception here");
    } catch (ConversionException expected) {
        assertEquals("Failed to convert [attach:Sandbox.WebHome@my.zip/a/b/c] to a Path object", expected.getMessage());
    }
}
Also used : TPath(net.java.truevfs.access.TPath) Path(java.nio.file.Path) ConversionException(org.xwiki.properties.converter.ConversionException) SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) URI(java.net.URI) Test(org.junit.Test)

Example 4 with ConversionException

use of org.xwiki.properties.converter.ConversionException in project xwiki-platform by xwiki.

the class PathConverterTest method convertWhenNoPermission.

@Test
public void convertWhenNoPermission() throws Exception {
    ResourceReferenceSerializer<VfsResourceReference, URI> serializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, URI.class), "truevfs");
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:Sandbox.WebHome@my.zip"), "a/b/c");
    when(serializer.serialize(reference)).thenReturn(URI.create("attach://xwiki:Sandbox.WebHome/my.zip/a/b/c"));
    VfsPermissionChecker permissionChecker = this.mocker.getInstance(VfsPermissionChecker.class, "cascading");
    doThrow(new VfsException("unauthorized")).when(permissionChecker).checkPermission(reference);
    try {
        this.mocker.getComponentUnderTest().convert(new DefaultParameterizedType(null, Path.class), "attach:Sandbox.WebHome@my.zip/a/b/c");
        fail("Should have thrown an exception here");
    } catch (ConversionException expected) {
        assertEquals("Failed to convert [attach:Sandbox.WebHome@my.zip/a/b/c] to a Path object", expected.getMessage());
        assertEquals("VfsException: unauthorized", ExceptionUtils.getRootCauseMessage(expected));
    }
}
Also used : VfsException(org.xwiki.vfs.VfsException) TPath(net.java.truevfs.access.TPath) Path(java.nio.file.Path) ConversionException(org.xwiki.properties.converter.ConversionException) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsPermissionChecker(org.xwiki.vfs.VfsPermissionChecker) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) URI(java.net.URI) Test(org.junit.Test)

Example 5 with ConversionException

use of org.xwiki.properties.converter.ConversionException in project xwiki-platform by xwiki.

the class PathConverter method convertToType.

@Override
protected Path convertToType(Type targetType, Object value) {
    if (value == null) {
        return null;
    }
    Path path;
    try {
        VfsResourceReference reference = new VfsResourceReference(new URI(value.toString()));
        // Verify that the user has the permission for the specified VFS scheme. We need to do this at this level
        // since it's possible to do the check in the driver itself since TrueVFS controls whether the driver is
        // called or not and does caching,
        // see https://java.net/projects/truezip/lists/users/archive/2015-12/message/8
        // Since this convert has to be called to use the VFS API from Velocity, we're safe that this will prevent
        // any Velocity script to execute a VFS call if the user is not allowed.
        // 
        // Note: Even though the user needs View access to the attachment, we cannot check this right now because
        // of the caching issue. However we consider that if the user has Programming Rights, he can do anything he
        // wants and thus it's safe that he can access the attachment.
        this.permissionChecker.checkPermission(reference);
        URI trueVfsURI = this.trueVfsResourceReferenceSerializer.serialize(reference);
        path = new TPath(trueVfsURI);
    } catch (Exception e) {
        throw new ConversionException(String.format("Failed to convert [%s] to a Path object", value), e);
    }
    return path;
}
Also used : TPath(net.java.truevfs.access.TPath) Path(java.nio.file.Path) ConversionException(org.xwiki.properties.converter.ConversionException) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) TPath(net.java.truevfs.access.TPath) URI(java.net.URI) ConversionException(org.xwiki.properties.converter.ConversionException)

Aggregations

ConversionException (org.xwiki.properties.converter.ConversionException)5 URI (java.net.URI)3 Path (java.nio.file.Path)3 TPath (net.java.truevfs.access.TPath)3 VfsResourceReference (org.xwiki.vfs.VfsResourceReference)3 Test (org.junit.Test)2 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)2 ResourceReferenceSerializer (org.xwiki.resource.ResourceReferenceSerializer)2 Message (javax.mail.Message)1 MimeMessage (javax.mail.internet.MimeMessage)1 WikiComponentRuntimeException (org.xwiki.component.wiki.WikiComponentRuntimeException)1 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)1 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)1 SerializeResourceReferenceException (org.xwiki.resource.SerializeResourceReferenceException)1 VfsException (org.xwiki.vfs.VfsException)1 VfsPermissionChecker (org.xwiki.vfs.VfsPermissionChecker)1