/*******************************************************************************
* Copyright (c) 2012-2015 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.api.vfs.server.impl.memory;
import org.eclipse.che.api.vfs.server.VirtualFile;
import org.eclipse.che.api.vfs.shared.dto.Principal;
import org.eclipse.che.api.vfs.shared.dto.VirtualFileSystemInfo.BasicPermissions;
import com.google.common.collect.Sets;
import org.everrest.core.impl.ContainerResponse;
import org.everrest.core.tools.ByteArrayContainerResponseWriter;
import javax.ws.rs.core.HttpHeaders;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/** @author andrew00x */
public class GetContentTest extends MemoryFileSystemTest {
private String fileId;
private String fileName;
private String folderId;
private String content = "__GetContentTest__";
private String filePath;
@Override
protected void setUp() throws Exception {
super.setUp();
String name = getClass().getName();
VirtualFile getContentTestFolder = mountPoint.getRoot().createFolder(name);
VirtualFile file =
getContentTestFolder.createFile("GetContentTest_FILE", "text/plain", new ByteArrayInputStream(content.getBytes()));
fileId = file.getId();
fileName = file.getName();
filePath = file.getPath();
VirtualFile folder = getContentTestFolder.createFolder("GetContentTest_FOLDER");
folderId = folder.getId();
}
public void testGetContent() throws Exception {
ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
String path = SERVICE_URI + "content/" + fileId;
ContainerResponse response = launcher.service("GET", path, BASE_URI, null, null, writer, null);
assertEquals(200, response.getStatus());
//log.info(new String(writer.getBody()));
assertEquals(content, new String(writer.getBody()));
assertEquals("text/plain", writer.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE));
}
public void testDownloadFile() throws Exception {
// Expect the same as 'get content' plus header "Content-Disposition".
ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
String path = SERVICE_URI + "downloadfile/" + fileId;
ContainerResponse response = launcher.service("GET", path, BASE_URI, null, null, writer, null);
assertEquals(200, response.getStatus());
//log.info(new String(writer.getBody()));
assertEquals(content, new String(writer.getBody()));
assertEquals("text/plain", writer.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE));
assertEquals("attachment; filename=\"" + fileName + "\"", writer.getHeaders().getFirst("Content-Disposition"));
}
public void testGetContentFolder() throws Exception {
ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
String path = SERVICE_URI + "content/" + folderId;
ContainerResponse response = launcher.service("GET", path, BASE_URI, null, null, writer, null);
assertEquals(403, response.getStatus());
log.info(new String(writer.getBody()));
}
public void testGetContentNoPermissions() throws Exception {
Principal adminPrincipal = createPrincipal("admin", Principal.Type.USER);
Map<Principal, Set<String>> permissions = new HashMap<>(1);
permissions.put(adminPrincipal, Sets.newHashSet(BasicPermissions.ALL.value()));
mountPoint.getVirtualFileById(fileId).updateACL(createAcl(permissions), true, null);
ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
String path = SERVICE_URI + "content/" + fileId;
ContainerResponse response = launcher.service("GET", path, BASE_URI, null, null, writer, null);
assertEquals(403, response.getStatus());
log.info(new String(writer.getBody()));
}
public void testGetContentByPath() throws Exception {
ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
String path = SERVICE_URI + "contentbypath" + filePath;
ContainerResponse response = launcher.service("GET", path, BASE_URI, null, null, writer, null);
assertEquals(200, response.getStatus());
//log.info(new String(writer.getBody()));
assertEquals(content, new String(writer.getBody()));
assertEquals("text/plain", writer.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE));
}
public void testGetContentByPathWithVersionID() throws Exception {
ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
String path = SERVICE_URI + "contentbypath" + filePath + '?' + "versionId=" + "0";
ContainerResponse response = launcher.service("GET", path, BASE_URI, null, null, writer, null);
assertEquals(200, response.getStatus());
//log.info(new String(writer.getBody()));
assertEquals(content, new String(writer.getBody()));
assertEquals("text/plain", writer.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE));
}
}