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