Java Code Examples for org.glassfish.jersey.server.ContainerResponse#getEntity()

The following examples show how to use org.glassfish.jersey.server.ContainerResponse#getEntity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Router.java    From minnal with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the http response from container response</p>
 * 
 * <li> Sets the content length to the http response from the container response. If content length is not available, computes from the response entity
 * <li> 
 * 
 * @param context
 * @param containerResponse
 * @param buffer
 * @return
 */
protected FullHttpResponse createHttpResponse(MessageContext context, ContainerResponse containerResponse, ByteBuf buffer) {
	FullHttpRequest httpRequest = context.getRequest();
	DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(httpRequest.getProtocolVersion(), HttpResponseStatus.valueOf(containerResponse.getStatus()), buffer);
	int length = containerResponse.getLength();
	
	// FIXME Hack. is there a better way?
	if (length == -1 && containerResponse.getEntity() instanceof String) {
		final String entity = (String) containerResponse.getEntity();
		final byte[] encodedBytes = entity.getBytes(Charset.forName("UTF-8"));
		length = encodedBytes.length;
	}
	if (! containerResponse.getHeaders().containsKey(HttpHeaders.Names.CONTENT_LENGTH)) {
		HttpHeaders.setContentLength(httpResponse, length);
		logger.trace("Writing response status and headers {}, length {}", containerResponse, containerResponse.getLength());
	}

	for (Map.Entry<String, List<Object>> headerEntry : containerResponse.getHeaders().entrySet()) {
		HttpHeaders.addHeader(httpResponse, headerEntry.getKey(), Joiner.on(", ").join(headerEntry.getValue()));
	}
	return httpResponse;
}
 
Example 2
Source File: DirectoryWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testTree() {
    ContainerResponse response = app.get("/api/directory/tree").execute();
    List<DirectoryNodeResponse> directories = (List<DirectoryNodeResponse>) response.getEntity();
    assertEquals(1, directories.size());
    DirectoryNodeResponse first = directories.get(0);
    assertEquals("/", first.directory.path);
    assertEquals(1, first.children.size());
    DirectoryNodeResponse second = first.children.get(0);
    assertEquals("/test/", second.directory.path);
    assertEquals(0, second.children.size());
}
 
Example 3
Source File: FileWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
void create() {
    CreateFileRequest request = new CreateFileRequest();
    request.directoryId = directoryId;
    request.path = "create";
    request.fileName = "create";
    request.description = "create";
    request.requestBy = UUID.randomUUID().toString();
    ContainerResponse containerResponse = app.post("/api/file").setEntity(request).execute();
    assertEquals(200, containerResponse.getStatus());
    FileResponse fileResponse = (FileResponse) containerResponse.getEntity();
    assertNotNull(fileResponse.id);
    assertEquals(request.path, fileResponse.path);
}
 
Example 4
Source File: FileWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
void list() {
    FileListQuery fileListQuery = new FileListQuery();
    fileListQuery.limit = 10;
    fileListQuery.page = 1;
    ContainerResponse containerResponse = app.put("/api/file/list").setEntity(fileListQuery).execute();
    assertEquals(200, containerResponse.getStatus());
    QueryResponse<FileListResponse> queryResponse = (QueryResponse) containerResponse.getEntity();
    assertTrue(queryResponse.total.intValue() >= 2);
    queryResponse.items.forEach(fileListResponse -> {
        if (!fileListResponse.isDirectory) {
            assertEquals(file.description, fileListResponse.description);
        }
    });
}
 
Example 5
Source File: FileWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void find() {
    FileQuery query = new FileQuery();
    query.page = 1;
    query.limit = 10;
    ContainerResponse response = app.put("/api/file").setEntity(query).execute();
    QueryResponse<FileResponse> result = (QueryResponse) response.getEntity();
    assertEquals(1, result.total.intValue());
    assertEquals("test", result.items.get(0).description);
}
 
Example 6
Source File: FileWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void findByPath() {
    ContainerResponse containerResponse = app.get("/api/file?path=" + file.path).execute();
    assertEquals(200, containerResponse.getStatus());
    Optional<FileResponse> optional = (Optional) containerResponse.getEntity();
    assertTrue(optional.isPresent());
    FileResponse fileResponse = optional.get();
    assertEquals(file.id, fileResponse.id);
    assertEquals(file.description, fileResponse.description);
}
 
Example 7
Source File: DirectoryWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testUpdate() {
    UpdateDirectoryRequest request = new UpdateDirectoryRequest();
    request.description = "description";
    request.requestBy = "test";
    ContainerResponse response = app.put("/api/directory/" + testDirectory.id).setEntity(request).execute();
    DirectoryResponse directoryResponse = (DirectoryResponse) response.getEntity();
    assertEquals("description", directoryResponse.description);
}
 
Example 8
Source File: DirectoryWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void createDirectories() {
    CreateDirectoriesRequest request = new CreateDirectoriesRequest();
    request.path = "/upload/dir/child/";
    ContainerResponse containerResponse = app.post("/api/directory/create").setEntity(request).execute();
    assertEquals(200, containerResponse.getStatus());
    DirectoryResponse directoryResponse = (DirectoryResponse) containerResponse.getEntity();
    assertNotNull(directoryResponse.parentId);
    assertEquals("/upload/dir/child/", directoryResponse.path);
}
 
Example 9
Source File: DirectoryWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
void create() {
    CreateDirectoryRequest request = new CreateDirectoryRequest();
    request.parentId = testDirectory.id;
    request.path = "/create/";
    request.description = "create";
    request.requestBy = UUID.randomUUID().toString();
    ContainerResponse containerResponse = app.post("/api/directory").setEntity(request).execute();
    assertEquals(200, containerResponse.getStatus());
    DirectoryResponse directoryResponse = (DirectoryResponse) containerResponse.getEntity();
    assertNotNull(directoryResponse.id);
    assertEquals(request.path, directoryResponse.path);
}
 
Example 10
Source File: DirectoryWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
void subTree() {
    ContainerResponse containerResponse = app.get("/api/directory/" + testDirectory.id + "/sub-tree").execute();
    assertEquals(200, containerResponse.getStatus());
    List<DirectoryNodeResponse> list = (List) containerResponse.getEntity();
    assertEquals(0, list.size());
}
 
Example 11
Source File: DirectoryWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
void firstTwoLevels() {
    ContainerResponse containerResponse = app.get("/api/directory/first-three-levels").execute();
    assertEquals(200, containerResponse.getStatus());
    List<DirectoryNodeResponse> list = (List) containerResponse.getEntity();
    assertEquals(1, list.size());
    assertEquals(1, list.get(0).children.size());
    assertEquals(testDirectory.id, list.get(0).children.get(0).directory.id);
}
 
Example 12
Source File: EndpointEnhancingRequestFilter.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Override
protected final Single<Response> handleContainerResponse(final ContainerResponse res) {
    if (!res.hasEntity()) {
        return super.handleContainerResponse(res);
    }

    final Object responseEntity = res.getEntity();
    return sourceType.isAssignableFrom(responseEntity.getClass()) ?
            handleSourceResponse(sourceType.cast(responseEntity), res) : super.handleContainerResponse(res);
}
 
Example 13
Source File: DirectoryWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
void children() {
    CreateDirectoryRequest request = new CreateDirectoryRequest();
    request.parentId = testDirectory.id;
    request.path = "/create";
    request.description = "create";
    request.requestBy = UUID.randomUUID().toString();
    DirectoryResponse child = directoryWebService.create(request);
    ContainerResponse containerResponse = app.get("/api/directory/" + testDirectory.id + "/children").execute();
    assertEquals(200, containerResponse.getStatus());
    List<String> list = (List) containerResponse.getEntity();
    assertEquals(1, list.size());
    assertEquals(child.id, list.get(0));
}
 
Example 14
Source File: DirectoryWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
void find() {
    DirectoryQuery query = new DirectoryQuery();
    query.page = 1;
    query.limit = 10;
    ContainerResponse containerResponse = app.put("/api/directory").setEntity(query).execute();
    assertEquals(200, containerResponse.getStatus());
    QueryResponse<DirectoryResponse> queryResponse = (QueryResponse) containerResponse.getEntity();
    assertEquals(2, queryResponse.total.intValue());
}
 
Example 15
Source File: FileWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void update() {
    UpdateFileRequest request = new UpdateFileRequest();
    request.description = "update";
    request.requestBy = "test";
    ContainerResponse response = app.put("/api/file/" + file.id).setEntity(request).execute();
    FileResponse fileResponse = (FileResponse) response.getEntity();
    assertEquals("update", fileResponse.description);
}
 
Example 16
Source File: PinCodeWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void create() {
    CreatePinCodeRequest request = new CreatePinCodeRequest();
    request.phone = "12334";
    request.requestBy = "web";
    request.ip = "127.0.0.1";
    ContainerResponse response = app.post("/api/pincode").setEntity(request).execute();
    assertEquals(200, response.getStatus());
    PinCodeResponse pinCodeResponse = (PinCodeResponse) response.getEntity();
    assertNotNull(pinCodeResponse.code);
}
 
Example 17
Source File: UserAutoLoginTokenWebServiceImplTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void create() {
    CreateUserAutoLoginTokenRequest request = new CreateUserAutoLoginTokenRequest();
    request.userId = UUID.randomUUID().toString();
    request.expireTime = OffsetDateTime.now();
    request.requestBy = "test";

    ContainerResponse response = app.post("/api/user/token").setEntity(request).execute();
    UserAutoLoginTokenResponse token = (UserAutoLoginTokenResponse) response.getEntity();
    assertNotNull(token.token);
}
 
Example 18
Source File: ServiceModuleTest.java    From jweb-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
void serviceSingleton() {
    AbstractModule module = app.module("service.test");
    TestService serviceImpl = module.require(TestService.class);
    ContainerResponse response = app.get("/api").execute();
    Map<String, Integer> entity = (Map<String, Integer>) response.getEntity();
    assertEquals(serviceImpl.hashCode(), (int) entity.get("hashCode"));
}
 
Example 19
Source File: ServiceModuleTest.java    From jweb-cms with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
void notFound() {
    ContainerResponse response = app.get("/api/not-found").execute();
    ExceptionResponse entity = (ExceptionResponse) response.getEntity();
    assertNotNull(entity.errorMessage);
}
 
Example 20
Source File: ServiceModuleTest.java    From jweb-cms with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
void defaultExceptionHandler() {
    ContainerResponse response = app.get("/api/exception").execute();
    ExceptionResponse entity = (ExceptionResponse) response.getEntity();
    assertNotNull(entity.errorMessage);
}