提交 8b3b750b 编写于 作者: F Frederik Heremans

ACT-1740: Replacing newlines for status-name

上级 3daabd55
......@@ -37,6 +37,10 @@ import org.restlet.service.StatusService;
*/
public class ActivitiStatusService extends StatusService {
protected static final String NEWLINE_REPLACE_REGEX = "\\r\\n|\\r|\\n";
/**
* Overriding this method to return a JSON-object representing the error that occurred instead of
* the default HTML body Restlet provides.
......@@ -73,22 +77,29 @@ public class ActivitiStatusService extends StatusService {
if(throwable instanceof ActivitiObjectNotFoundException) {
// 404 - Entity not found
status = new Status(Status.CLIENT_ERROR_NOT_FOUND.getCode(), throwable.getMessage(), null, null);
status = new Status(Status.CLIENT_ERROR_NOT_FOUND.getCode(), getSafeStatusName(throwable.getMessage()), null, null);
} else if(throwable instanceof ActivitiIllegalArgumentException) {
// 400 - Bad Request
status = new Status(Status.CLIENT_ERROR_BAD_REQUEST.getCode(), throwable.getMessage(), null, null);
status = new Status(Status.CLIENT_ERROR_BAD_REQUEST.getCode(), getSafeStatusName(throwable.getMessage()), null, null);
} else if (throwable instanceof ActivitiOptimisticLockingException || throwable instanceof ActivitiTaskAlreadyClaimedException) {
// 409 - Conflict
status = new Status(Status.CLIENT_ERROR_CONFLICT.getCode(), throwable.getMessage(), null, null);
status = new Status(Status.CLIENT_ERROR_CONFLICT.getCode(), getSafeStatusName(throwable.getMessage()), null, null);
} else if (throwable instanceof ResourceException) {
ResourceException re = (ResourceException) throwable;
status = re.getStatus();
} else if(throwable instanceof ActivitiException) {
status = new Status(Status.SERVER_ERROR_INTERNAL.getCode(), throwable.getMessage(), null, null);;
status = new Status(Status.SERVER_ERROR_INTERNAL.getCode(), getSafeStatusName(throwable.getMessage()), null, null);;
} else {
status = null;
}
return status;
}
protected String getSafeStatusName(String name) {
if(name != null) {
return name.replaceAll(NEWLINE_REPLACE_REGEX, " ");
}
return null;
}
}
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.rest;
import junit.framework.TestCase;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.ActivitiOptimisticLockingException;
import org.activiti.engine.ActivitiTaskAlreadyClaimedException;
import org.activiti.rest.application.ActivitiStatusService;
import org.restlet.data.Status;
import org.restlet.service.StatusService;
/**
* @author Frederik Heremans
*/
public class ActivitiStatusServiceTest extends TestCase {
private StatusService statusService;
@Override
protected void setUp() throws Exception {
statusService = new ActivitiStatusService();
}
public void testNewlineInMessage() throws Exception {
Status status = statusService.getStatus(new ActivitiException("This is a\n newline"), null);
assertNotNull(status);
assertEquals("This is a newline", status.getName());
}
public void test404WhenNotFound() throws Exception {
Status status = statusService.getStatus(new ActivitiObjectNotFoundException(String.class), null);
assertNotNull(status);
assertEquals(Status.CLIENT_ERROR_NOT_FOUND, status);
}
public void test400WhenIllegalArgument() throws Exception {
Status status = statusService.getStatus(new ActivitiIllegalArgumentException("testing"), null);
assertNotNull(status);
assertEquals(Status.CLIENT_ERROR_BAD_REQUEST, status);
}
public void test409WhenConflict() throws Exception {
Status status = statusService.getStatus(new ActivitiTaskAlreadyClaimedException("id", "assignee"), null);
assertNotNull(status);
assertEquals(Status.CLIENT_ERROR_CONFLICT, status);
status = statusService.getStatus(new ActivitiOptimisticLockingException("testing"), null);
assertNotNull(status);
assertEquals(Status.CLIENT_ERROR_CONFLICT, status);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册