Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@
package org.restlet.test.resource;

import java.io.IOException;
import java.util.Map;

import org.restlet.Application;
import org.restlet.engine.Engine;
import org.restlet.ext.jackson.JacksonConverter;
import org.restlet.ext.jackson.JacksonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.StatusRepresentation;
import org.restlet.resource.ClientResource;
import org.restlet.resource.Finder;
import org.restlet.resource.ResourceException;
import org.restlet.test.RestletTestCase;

Expand All @@ -59,11 +63,13 @@ protected void setUp() throws Exception {
Engine.getInstance().getRegisteredConverters()
.add(new JacksonConverter());
Engine.getInstance().registerDefaultConverters();
Finder finder = new Finder();
finder.setTargetClass(MyServerResource20.class);

//Use Application because we need StatusService, ConverterService, ...
Application application = new Application();
application.setInboundRoot(MyServerResource20.class);

this.clientResource = new ClientResource("http://local");
this.clientResource.setNext(finder);
this.clientResource.setNext(application);
this.myResource = clientResource.wrap(MyResource20.class);
}

Expand All @@ -77,9 +83,39 @@ protected void tearDown() throws Exception {
public void testGet() throws IOException, ResourceException {
try {
myResource.represent();
} catch (MyException e) {
assertNotNull(e.getDate());
fail("should fail");
} catch (MyException01 e) {
fail("exception should be catch by client resource");
} catch (ResourceException e) {
assertEquals(400, e.getStatus().getCode());
Representation responseEntity = clientResource.getResponseEntity();
if (responseEntity instanceof JacksonRepresentation) {
assertTrue(JacksonRepresentation.class.isAssignableFrom(responseEntity.getClass()));
JacksonRepresentation jacksonRepresentation = (JacksonRepresentation) responseEntity;
Object entity = jacksonRepresentation.getObject();
assertTrue(StatusRepresentation.class.isAssignableFrom(entity.getClass()));
StatusRepresentation statusRepresentation = (StatusRepresentation) entity;
assertEquals(400, statusRepresentation.getCode());
}
}
}


public void testGetAndSerializeException() throws IOException, ResourceException {
try {
myResource.representAndSerializeException();
fail("should fail");
} catch (MyException02 e) {
fail("exception should be catch by client resource");
} catch (ResourceException e) {
assertEquals(400, e.getStatus().getCode());
Representation responseEntity = clientResource.getResponseEntity();
assertTrue(JacksonRepresentation.class.isAssignableFrom(responseEntity.getClass()));
JacksonRepresentation jacksonRepresentation = (JacksonRepresentation) responseEntity;
Object entity = jacksonRepresentation.getObject();
assertTrue(Map.class.isAssignableFrom(entity.getClass()));
Map<String, Object> map = (Map<String, Object>) entity;
assertEquals("my custom error", map.get("customProperty"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

import org.restlet.resource.Status;

@Status("401")
public class MyException extends Throwable {
@Status(400)
public class MyException01 extends Throwable {

private static final long serialVersionUID = 1L;

private Date date;

public MyException(Date date) {
public MyException01(Date date) {
this.date = date;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.restlet.test.resource;

import org.restlet.resource.Status;

import java.util.Date;

@Status(value = 400, serializeProperties = true)
public class MyException02 extends Throwable {

private static final long serialVersionUID = 1L;

private String customProperty;

public MyException02(String customProperty) {
this.customProperty = customProperty ;
}

public String getCustomProperty() {
return customProperty;
}

public void setCustomProperty(String customProperty) {
this.customProperty = customProperty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
package org.restlet.test.resource;

import org.restlet.resource.Get;
import org.restlet.resource.Put;

/**
* Sample annotated interface.
Expand All @@ -43,6 +44,9 @@
public interface MyResource20 {

@Get
MyBean represent() throws MyException;
MyBean represent() throws MyException01;

@Put
MyBean representAndSerializeException() throws MyException02;

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@ public static void main(String[] args) throws Exception {
server.start();
}

private volatile MyBean myBean = new MyBean("myName", "myDescription");

@SuppressWarnings("unused")
public MyBean represent() throws MyException {
if (true) {
throw new MyException(new Date());
}
public MyBean represent() throws MyException01 {
throw new MyException01(new Date());
}

return myBean;
@Override
public MyBean representAndSerializeException() throws MyException02 {
throw new MyException02("my custom error");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,24 @@

package org.restlet.test.service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;

import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.ext.jackson.JacksonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.service.ConnegService;
import org.restlet.service.ConverterService;
import org.restlet.service.MetadataService;
import org.restlet.service.StatusService;
import org.restlet.test.RestletTestCase;
import org.restlet.test.resource.MyException;
import org.restlet.test.resource.MyException01;
import org.restlet.test.resource.MyException02;

/**
* Unit tests for the status service.
Expand All @@ -49,8 +61,61 @@ public class StatusServiceTestCase extends RestletTestCase {

public void testAnnotation() {
StatusService ss = new StatusService();
Status status = ss.toStatus(new MyException(new Date()), null, null);
assertEquals(401, status.getCode());
Status status = ss.toStatus(new MyException01(new Date()), null, null);
assertEquals(400, status.getCode());
}

public void testRepresentation() throws IOException {
StatusService ss = new StatusService();
Status status = new Status(400, new MyException01(new Date()));

ConverterService converterService = new ConverterService();
ConnegService connegService = new ConnegService();
MetadataService metadataService = new MetadataService();

Request request = new Request();
Representation representation = ss.toRepresentation(status, null,
request, new Response(request),
converterService, connegService, metadataService);

//verify
Status expectedStatus = Status.CLIENT_ERROR_BAD_REQUEST;
HashMap<String, Object> expectedRepresentationMap = new LinkedHashMap<String, Object>();
expectedRepresentationMap.put("code", expectedStatus.getCode());
expectedRepresentationMap.put("reasonPhrase", expectedStatus.getReasonPhrase());
expectedRepresentationMap.put("description", expectedStatus.getDescription());
String expectedJsonRepresentation =
new JacksonRepresentation<HashMap<String, Object>>(expectedRepresentationMap)
.getText();

Status.CLIENT_ERROR_BAD_REQUEST.getCode();
assertEquals(MediaType.APPLICATION_JSON, representation.getMediaType());
assertEquals(expectedJsonRepresentation, representation.getText());
}

public void testRepresentationWithExceptionPropertiesSerialized() throws IOException {
StatusService ss = new StatusService();
Status status = new Status(400, new MyException02("test message"));

ConverterService converterService = new ConverterService();
ConnegService connegService = new ConnegService();
MetadataService metadataService = new MetadataService();

Request request = new Request();
Representation representation = ss.toRepresentation(status, null,
request, new Response(request),
converterService, connegService, metadataService);

//verify
HashMap<String, Object> expectedRepresentationMap = new LinkedHashMap<String, Object>();
expectedRepresentationMap.put("customProperty", "test message");
String expectedJsonRepresentation =
new JacksonRepresentation<HashMap<String, Object>>(expectedRepresentationMap)
.getText();

Status.CLIENT_ERROR_BAD_REQUEST.getCode();
assertEquals(MediaType.APPLICATION_JSON, representation.getMediaType());
assertEquals(expectedJsonRepresentation, representation.getText());
}

}
4 changes: 4 additions & 0 deletions modules/org.restlet/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<exclude name="src/org/restlet/engine/adapter/HttpServer*.java" />
<exclude name="src/org/restlet/engine/adapter/Server*.java" />
<exclude name="src/org/restlet/engine/application/*Conneg.java" />
<exclude name="src/org/restlet/engine/application/StatusFilter.java" />
<exclude name="src/org/restlet/engine/component/**" />
<exclude name="src/org/restlet/engine/connector/ConnectionClosingRepresentation*" />
<exclude name="src/org/restlet/engine/connector/Ftp*" />
Expand Down Expand Up @@ -118,6 +119,7 @@
<exclude name="src/org/restlet/engine/ssl/**" />
<exclude name="src/org/restlet/engine/util/AlphabeticalComparator.java" />
<exclude name="src/org/restlet/engine/util/AlphaNumericComparator.java" />
<exclude name="src/org/restlet/engine/util/BeanInfoUtils.java" />
<exclude name="src/org/restlet/engine/util/CallResolver.java" />
<exclude name="src/org/restlet/engine/util/ChildClientDispatcher.java" />
<exclude name="src/org/restlet/engine/util/ChildContext.java" />
Expand All @@ -134,6 +136,7 @@
<exclude name="src/org/restlet/engine/util/Pool.java" />
<exclude name="src/org/restlet/engine/util/ReferenceUtils.java" />
<exclude name="src/org/restlet/engine/util/TemplateDispatcher.java" />
<exclude name="src/org/restlet/engine/util/ThrowableSerializer.java" />
<exclude name="src/org/restlet/engine/util/WrapperScheduledExecutorService.java" />
<exclude name="src/org/restlet/representation/AppendableRepresentation.java" />
<exclude name="src/org/restlet/representation/BufferingRepresentation.java" />
Expand Down Expand Up @@ -164,6 +167,7 @@
<exclude name="src/org/restlet/service/LogService.java" />
<exclude name="src/org/restlet/service/RangeService.java" />
<exclude name="src/org/restlet/service/RealmService.java" />
<exclude name="src/org/restlet/service/StatusService.java" />
<exclude name="src/org/restlet/service/TaskService.java" />
<exclude name="src/org/restlet/service/TunnelService.java" />
<exclude name="src/org/restlet/util/ByteReadingListener.java" />
Expand Down
3 changes: 1 addition & 2 deletions modules/org.restlet/src/org/restlet/data/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import org.restlet.engine.Edition;
import org.restlet.engine.Engine;
import org.restlet.service.StatusService;

/**
* Status to return after handling a call.
Expand Down Expand Up @@ -1092,7 +1091,7 @@ public int getCode() {

/**
* Returns the description. This value is typically used by the
* {@link StatusService} to build a meaningful description of an error via a
* {@link org.restlet.service.StatusService} to build a meaningful description of an error via a
* response entity.
*
* @return The description.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.routing.Filter;
import org.restlet.service.ConnegService;
import org.restlet.service.ConverterService;
import org.restlet.service.MetadataService;
import org.restlet.service.StatusService;

// [excludes gwt]
Expand Down Expand Up @@ -176,7 +178,9 @@ protected int doHandle(Request request, Response response) {
if ((response.getEntity() == null) || isOverwriting()) {
response.setEntity(getStatusService().toRepresentation(
status, throwable, request, response,
getApplication().getConverterService()));
getConverterService(),
getConnegService(),
getMetadataService()));
}
}
}
Expand Down Expand Up @@ -204,6 +208,26 @@ public ConverterService getConverterService() {
.getConverterService();
}

/**
* Returns the content negotiation service of the application if available or null.
*
* @return The content negotiation service of the application if available or null.
*/
public ConnegService getConnegService() {
return (getApplication() == null) ? null : getApplication()
.getConnegService();
}

/**
* Returns the metadata service of the application if available or null.
*
* @return The metadata service of the application if available or null.
*/
public MetadataService getMetadataService() {
return (getApplication() == null) ? null : getApplication()
.getMetadataService();
}

/**
* Returns a representation for the given status.<br>
* In order to customize the default representation, this method can be
Expand Down Expand Up @@ -279,7 +303,7 @@ public Reference getHomeRef() {
* @param response
* The response updated.
* @return The representation of the given status.
* @deprecated Use {@link #toRepresentation(Status, Request, Response)}
* @deprecated Use {@link #toRepresentation(Status, Throwable, Request, Response)}
* instead.
*/
@Deprecated
Expand Down Expand Up @@ -403,7 +427,7 @@ protected Representation toRepresentation(Status status,

try {
result = getStatusService().toRepresentation(status, throwable,
request, response, getConverterService());
request, response, getConverterService(), getConnegService(), getMetadataService());
} catch (Exception e) {
getLogger().log(Level.WARNING,
"Unable to get the custom status representation", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* Alternatively, you can obtain a royalty free commercial license with less
* limitations, transferable or non-transferable, directly at
* http://restlet.com/products/restlet-framework
*
*
* Restlet is a registered trademark of Restlet S.A.S.
*/

Expand All @@ -44,6 +44,7 @@
import org.restlet.data.Method;
import org.restlet.representation.Representation;
import org.restlet.resource.ServerResource;
import org.restlet.resource.Status;
import org.restlet.service.MetadataService;

// [excludes gwt]
Expand Down Expand Up @@ -147,8 +148,9 @@ private List<AnnotationInfo> addStatusAnnotationDescriptors(
.getAnnotation(org.restlet.resource.Status.class);

if (annotation != null) {
Status status = (Status) annotation;
result.add(new StatusAnnotationInfo(initialClass,
((org.restlet.resource.Status) annotation).value()));
status.value(), status.serializeProperties()));
}

return result;
Expand Down
Loading