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
10 changes: 9 additions & 1 deletion build/tmpl/text/changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ Changes log

- Bugs fixed
- @Status now works with annotated methods on the server-side.

- API changes
- Deprecated statusService property in Component to keep it only in Application.
- Added connegService, metadataService and converterService properties to StatusService
and pass them via the constructor instead of the toRepresentation and toStatus methods.
- Renamed @Status#serializeProperties (default to false) into serialize (default to true).
- Deprecated Component#statusService in favor of the Application#statusService property.


- 2.3 Milestone 3 (09/18/2014)
- New features
Expand All @@ -36,7 +44,7 @@ Changes log
methods accepting 'null' string values to facilitate the processing of path
variables and query parameters.
- Renamed StatusService methods from getRepresentation(...) and getStatus(...) to
toRepresentation(...) and toStatus(...), added additional parameters to allow
toRepresentation(...) and toStatus(...), added throwable parameters to allow
access to throwable and converter service if available.
- Aligned behavior of StatusService (underlying StatusFilter) with the behavior of
the ServerResource#doCatch() method in case a throwable is intercepted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ protected ServerCall createCall(Server server, HttpServletRequest request,
*
* @return The newly created Component or null if unable to create.
*/
@SuppressWarnings("deprecation")
protected Component createComponent() {
// Detect both customized Component and configuration with restlet.xml
// file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

package org.restlet.test.ext.sip;

import java.io.IOException;

import org.junit.Test;
import org.restlet.data.Parameter;
import org.restlet.data.Reference;
Expand Down Expand Up @@ -97,7 +99,7 @@ public void testParsing() throws Exception {
}

@Test
public void testWriting() {
public void testWriting() throws IOException {
Address a = new Address();
a.setDisplayName("A. G. Bell");
a.setReference(new Reference("sip:agb@bell-telephone.com"));
Expand All @@ -114,6 +116,7 @@ public void testWriting() {
a.getParameters().add("tag", "a48s");
assertEquals("<sip:agb@bell-telephone.com> ;tag=a48s", w.append(a)
.toString());
w.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@
package org.restlet.test.resource;

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

import org.restlet.Application;
import org.restlet.data.MediaType;
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.StatusInfo;
import org.restlet.resource.ClientResource;
import org.restlet.resource.Finder;
import org.restlet.resource.ResourceException;
import org.restlet.test.RestletTestCase;

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

// Hosts resources into an Application because we need some services for
// handling content negotiation, conversion of exceptions, etc.
Application application = new Application();
application.setInboundRoot(MyServerResource20.class);

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

Expand All @@ -77,9 +86,42 @@ 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 caught 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(StatusInfo.class.isAssignableFrom(entity
.getClass()));
StatusInfo statusInfo = (StatusInfo) entity;
assertEquals(400, statusInfo.getCode());
}
}
}

public void testGetAndSerializeException() throws IOException,
ResourceException {
try {
myResource.representAndSerializeException();
fail("Should fail");
} catch (MyException02 e) {
fail("Exception should be caught 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(value = 400, serialize = false)
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,23 @@
package org.restlet.test.resource;

import org.restlet.resource.Status;

@Status(value = 400)
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,13 @@ 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,62 @@ 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 {
Status status = new Status(400, new MyException01(new Date()));

ConverterService converterService = new ConverterService();
ConnegService connegService = new ConnegService();
MetadataService metadataService = new MetadataService();
StatusService ss = new StatusService(true, converterService,
metadataService, connegService);

Request request = new Request();
Representation representation = ss.toRepresentation(status,
request, new Response(request));

// 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 testSerializedException()
throws IOException {
Status status = new Status(400, new MyException02("test message"));

ConverterService converterService = new ConverterService();
ConnegService connegService = new ConnegService();
MetadataService metadataService = new MetadataService();
StatusService ss = new StatusService(true, converterService,
metadataService, connegService);

Request request = new Request();
Representation representation = ss.toRepresentation(status,
request, new Response(request));

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

}
3 changes: 3 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
13 changes: 9 additions & 4 deletions modules/org.restlet/src/org/restlet/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,24 @@ public Application(Context context) {
this.helper.setContext(context);
}

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

this.outboundRoot = null;
this.inboundRoot = null;
this.roles = new CopyOnWriteArrayList<Role>();
this.services = new ServiceList(context);
this.services.add(new TunnelService(true, true));
this.services.add(new StatusService());
this.services.add(new StatusService(true, converterService,
metadataService, connegService));
this.services.add(new DecoderService());
this.services.add(new EncoderService(false));
this.services.add(new RangeService());
this.services.add(new ConnectorService());
this.services.add(new ConnegService());
this.services.add(new ConverterService());
this.services.add(new MetadataService());
this.services.add(connegService);
this.services.add(converterService);
this.services.add(metadataService);

// [ifndef gae]
this.services.add(new org.restlet.service.TaskService(false));
Expand Down
5 changes: 5 additions & 0 deletions modules/org.restlet/src/org/restlet/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,9 @@ public ServiceList getServices() {
* Returns the status service, enabled by default.
*
* @return The status service.
* @deprecated Use {@link Application#getStatusService()} instead.
*/
@Deprecated
public StatusService getStatusService() {
return getServices().get(StatusService.class);
}
Expand Down Expand Up @@ -530,7 +532,10 @@ public void setServers(ServerList servers) {
*
* @param statusService
* The status service.
* @deprecated Use {@link Application#setStatusService(StatusService)}
* instead.
*/
@Deprecated
public void setStatusService(StatusService statusService) {
getServices().set(statusService);
}
Expand Down
Loading