Skip to content

Commit

Permalink
Use HTTP constants instead of hard-coded strings
Browse files Browse the repository at this point in the history
Content-Type, application/<type>, test/plain, Content-Disposition, Method

Added ExtMediaType to commons-lang for extended MediaType constants

Signed-off-by: Tareq Sharafy <tareq.sharafy@sap.com>
  • Loading branch information
tareksha committed Jun 25, 2015
1 parent 7eb3090 commit e642936
Show file tree
Hide file tree
Showing 100 changed files with 1,233 additions and 956 deletions.
Expand Up @@ -49,6 +49,7 @@
import org.eclipse.che.commons.lang.cache.Cache;
import org.eclipse.che.commons.lang.cache.LoadingValueSLRUCache;
import org.eclipse.che.commons.lang.cache.SynchronizedCache;
import org.eclipse.che.commons.lang.ws.rs.ExtMediaType;
import org.eclipse.che.dto.server.DtoFactory;

import com.google.common.annotations.Beta;
Expand Down Expand Up @@ -1137,7 +1138,7 @@ ContentStream zip(VirtualFileImpl virtualFile, VirtualFileFilter filter) throws
}
closeQuietly(zipOut);
final String name = virtualFile.getName() + ".zip";
return new ContentStream(name, new DeleteOnCloseFileInputStream(zipFile), "application/zip", zipFile.length(), new Date());
return new ContentStream(name, new DeleteOnCloseFileInputStream(zipFile), ExtMediaType.APPLICATION_ZIP, zipFile.length(), new Date());
} catch (IOException | RuntimeException ioe) {
if (zipFile != null) {
zipFile.delete();
Expand Down
Expand Up @@ -14,8 +14,8 @@
import org.eclipse.che.api.vfs.shared.dto.Principal;
import org.eclipse.che.commons.env.EnvironmentContext;
import org.eclipse.che.commons.user.UserImpl;

import org.eclipse.che.dto.server.DtoFactory;

import com.google.common.collect.Sets;

import org.everrest.core.impl.ContainerResponse;
Expand All @@ -32,6 +32,10 @@
import java.util.Map;
import java.util.Set;

import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;

import static org.eclipse.che.api.vfs.shared.dto.VirtualFileSystemInfo.BasicPermissions;

/** @author <a href="mailto:andrew00x@gmail.com">Andrey Parfonov</a> */
Expand Down Expand Up @@ -72,7 +76,7 @@ protected void setUp() throws Exception {
public void testGetACL() throws Exception {
ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
String requestPath = SERVICE_URI + "acl/" + fileId;
ContainerResponse response = launcher.service("GET", requestPath, BASE_URI, null, null, writer, null);
ContainerResponse response = launcher.service(HttpMethod.GET, requestPath, BASE_URI, null, null, writer, null);
log.info(new String(writer.getBody()));
assertEquals("Error: " + response.getEntity(), 200, response.getStatus());
@SuppressWarnings("unchecked")
Expand All @@ -89,7 +93,7 @@ public void testGetACLNoPermissions() throws Exception {
// Request must fail since we have not permissions any more to read ACL.
ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
String requestPath = SERVICE_URI + "acl/" + fileId;
ContainerResponse response = launcher.service("GET", requestPath, BASE_URI, null, null, writer, null);
ContainerResponse response = launcher.service(HttpMethod.GET, requestPath, BASE_URI, null, null, writer, null);
log.info(new String(writer.getBody()));
assertEquals(403, response.getStatus());
}
Expand All @@ -100,8 +104,8 @@ public void testUpdateACL() throws Exception {
// Give write permission for john. No changes for other users.
String acl = "[{\"principal\":{\"name\":\"john\",\"type\":\"USER\"},\"permissions\":[\"read\", \"write\"]}]";
Map<String, List<String>> h = new HashMap<>(1);
h.put("Content-Type", Arrays.asList("application/json"));
ContainerResponse response = launcher.service("POST", requestPath, BASE_URI, h, acl.getBytes(), null);
h.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON));
ContainerResponse response = launcher.service(HttpMethod.POST, requestPath, BASE_URI, h, acl.getBytes(), null);
assertEquals(204, response.getStatus());

Principal principal = DtoFactory.getInstance().createDto(Principal.class).withName("john").withType(Principal.Type.USER);
Expand All @@ -112,7 +116,7 @@ public void testUpdateACL() throws Exception {

// check API
assertEquals(permissions,
toMap((List<AccessControlEntry>)launcher.service("GET", requestPath, BASE_URI, null, null, null).getEntity()));
toMap((List<AccessControlEntry>)launcher.service(HttpMethod.GET, requestPath, BASE_URI, null, null, null).getEntity()));
}

@SuppressWarnings("unchecked")
Expand All @@ -121,8 +125,8 @@ public void testUpdateACLOverride() throws Exception {
// Give 'all' rights to admin and take away all rights for other users.
String acl = "[{\"principal\":{\"name\":\"admin\",\"type\":\"USER\"},\"permissions\":[\"all\"]}]";
Map<String, List<String>> h = new HashMap<>(1);
h.put("Content-Type", Arrays.asList("application/json"));
ContainerResponse response = launcher.service("POST", requestPath, BASE_URI, h, acl.getBytes(), null);
h.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON));
ContainerResponse response = launcher.service(HttpMethod.POST, requestPath, BASE_URI, h, acl.getBytes(), null);
assertEquals(204, response.getStatus());

Principal user1 = DtoFactory.getInstance().createDto(Principal.class).withName("andrew").withType(Principal.Type.USER);
Expand All @@ -136,7 +140,7 @@ public void testUpdateACLOverride() throws Exception {

// check API
assertEquals(permissions,
toMap((List<AccessControlEntry>)launcher.service("GET", requestPath, BASE_URI, null, null, null).getEntity()));
toMap((List<AccessControlEntry>)launcher.service(HttpMethod.GET, requestPath, BASE_URI, null, null, null).getEntity()));
}

@SuppressWarnings("unchecked")
Expand All @@ -145,16 +149,16 @@ public void testRemoveACL() throws Exception {
String requestPath = SERVICE_URI + "acl/" + fileId + '?' + "override=" + true;
String acl = "[]";
Map<String, List<String>> h = new HashMap<>(1);
h.put("Content-Type", Arrays.asList("application/json"));
ContainerResponse response = launcher.service("POST", requestPath, BASE_URI, h, acl.getBytes(), null);
h.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON));
ContainerResponse response = launcher.service(HttpMethod.POST, requestPath, BASE_URI, h, acl.getBytes(), null);
assertEquals(204, response.getStatus());

// check backend
assertNull(readPermissions(filePath));

// check API
List<AccessControlEntry> updatedAcl =
(List<AccessControlEntry>)launcher.service("GET", requestPath, BASE_URI, null, null, null).getEntity();
(List<AccessControlEntry>)launcher.service(HttpMethod.GET, requestPath, BASE_URI, null, null, null).getEntity();
// TODO: test files because we always provide "default ACL" at the moment.
// It is temporary solution before we get client side tool to manage ACL.
// assertTrue(updatedAcl.isEmpty());
Expand All @@ -171,11 +175,11 @@ public void testUpdateACLHavePermissions() throws Exception {
// Give write permission for john. No changes for other users.
String acl = "[{\"principal\":{\"name\":\"admin\",\"type\":\"USER\"},\"permissions\":[\"read\", \"write\"]}]";
Map<String, List<String>> h = new HashMap<>(1);
h.put("Content-Type", Arrays.asList("application/json"));
h.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON));
// File is protected and default principal 'andrew' has not update_acl permission.
// Replace default principal by principal who has write permission.
EnvironmentContext.getCurrent().setUser(new UserImpl("andrew", "andrew", null, Arrays.asList("workspace/developer"), false));
ContainerResponse response = launcher.service("POST", requestPath, BASE_URI, h, acl.getBytes(), null);
ContainerResponse response = launcher.service(HttpMethod.POST, requestPath, BASE_URI, h, acl.getBytes(), null);
assertEquals(204, response.getStatus());

principal = DtoFactory.getInstance().createDto(Principal.class).withName("admin").withType(Principal.Type.USER);
Expand All @@ -186,7 +190,7 @@ public void testUpdateACLHavePermissions() throws Exception {

// check API
assertEquals(permissions,
toMap((List<AccessControlEntry>)launcher.service("GET", requestPath, BASE_URI, null, null, null).getEntity()));
toMap((List<AccessControlEntry>)launcher.service(HttpMethod.GET, requestPath, BASE_URI, null, null, null).getEntity()));
}

@SuppressWarnings("unchecked")
Expand All @@ -198,12 +202,12 @@ public void testUpdateACLNoPermissions() throws Exception {

String acl = "[{\"principal\":{\"name\":\"admin\",\"type\":\"USER\"},\"permissions\":[\"all\"]}]";
Map<String, List<String>> h = new HashMap<>(1);
h.put("Content-Type", Arrays.asList("application/json"));
h.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON));

// Request must fail since we have not permissions any more to update ACL.
ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
String requestPath = SERVICE_URI + "acl/" + fileId;
ContainerResponse response = launcher.service("POST", requestPath, BASE_URI, h, acl.getBytes(), writer, null);
ContainerResponse response = launcher.service(HttpMethod.POST, requestPath, BASE_URI, h, acl.getBytes(), writer, null);
assertEquals(403, response.getStatus());
log.info(new String(writer.getBody()));

Expand All @@ -214,7 +218,7 @@ public void testUpdateACLNoPermissions() throws Exception {

// check API
assertEquals(permissions,
toMap((List<AccessControlEntry>)launcher.service("GET", requestPath, BASE_URI, null, null, null).getEntity()));
toMap((List<AccessControlEntry>)launcher.service(HttpMethod.GET, requestPath, BASE_URI, null, null, null).getEntity()));
}

@SuppressWarnings("unchecked")
Expand All @@ -223,10 +227,10 @@ public void testUpdateACLLocked() throws Exception {
"{\"principal\":{\"name\":\"any\",\"type\":\"USER\"},\"permissions\":null}," +
"{\"principal\":{\"name\":\"admin\",\"type\":\"USER\"},\"permissions\":[\"read\"]}]";
Map<String, List<String>> h = new HashMap<>(1);
h.put("Content-Type", Arrays.asList("application/json"));
h.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON));

String requestPath = SERVICE_URI + "acl/" + lockedFileId + '?' + "lockToken=" + lockToken + "&override=" + true;
ContainerResponse response = launcher.service("POST", requestPath, BASE_URI, h, acl.getBytes(), null);
ContainerResponse response = launcher.service(HttpMethod.POST, requestPath, BASE_URI, h, acl.getBytes(), null);

assertEquals(204, response.getStatus());

Expand All @@ -242,7 +246,7 @@ public void testUpdateACLLocked() throws Exception {

// check API
assertEquals(thisTestAccessList,
toMap((List<AccessControlEntry>)launcher.service("GET", requestPath, BASE_URI, null, null, null).getEntity()));
toMap((List<AccessControlEntry>)launcher.service(HttpMethod.GET, requestPath, BASE_URI, null, null, null).getEntity()));
}

@SuppressWarnings("unchecked")
Expand All @@ -251,16 +255,16 @@ public void testUpdateACLLockedNoLockToken() throws Exception {
"{\"principal\":{\"name\":\"any\",\"type\":\"USER\"},\"permissions\":null}," +
"{\"principal\":{\"name\":\"admin\",\"type\":\"USER\"},\"permissions\":[\"read\"]}]";
Map<String, List<String>> h = new HashMap<>(1);
h.put("Content-Type", Arrays.asList("application/json"));
h.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON));

String requestPath = SERVICE_URI + "acl/" + lockedFileId;
ContainerResponse response = launcher.service("POST", requestPath, BASE_URI, h, acl.getBytes(), null);
ContainerResponse response = launcher.service(HttpMethod.POST, requestPath, BASE_URI, h, acl.getBytes(), null);

assertEquals(403, response.getStatus());

// ACL must not be updated.
List<AccessControlEntry> updatedAcl =
(List<AccessControlEntry>)launcher.service("GET", requestPath, BASE_URI, null, null, null).getEntity();
(List<AccessControlEntry>)launcher.service(HttpMethod.GET, requestPath, BASE_URI, null, null, null).getEntity();
assertTrue(updatedAcl.isEmpty()); // TODO
}

Expand Down

0 comments on commit e642936

Please sign in to comment.