import static org.fest.assertions.Assertions.assertThat; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.auth.AuthenticationException; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.DefaultHttpClient; import org.junit.Test; import org.modeshape.common.util.Logger; import org.modeshape.web.jcr.rest.client.IRestClient; import org.modeshape.web.jcr.rest.client.Status; import org.modeshape.web.jcr.rest.client.domain.Repository; import org.modeshape.web.jcr.rest.client.domain.Server; import org.modeshape.web.jcr.rest.client.domain.Workspace; import org.modeshape.web.jcr.rest.client.json.JsonRestClient; public class FileManagerTest { private static final Logger LOGGER = Logger.getLogger(FileManagerTest.class); private static final boolean ALWAYS_USE_VERSIONING = true; private static String url = "http://localhost:8080/resources"; @Test public void save() throws Exception { Workspace workspace = getWorkspace(); IRestClient restClient = new JsonRestClient(); String path = "/"; File file = File.createTempFile("tmp", "file"); Status status = restClient.publish(workspace, path, file, ALWAYS_USE_VERSIONING); assertThat(status.isOk()).isTrue(); String fileContentUrl = url + "/repository/default/items/" + file.getName() + "/jcr:content"; String responseForContent = getResponse(fileContentUrl); assertThat(responseForContent).contains("mix:versionable"); String fileUrl = url + "/repository/default/items/" + file.getName(); String responseForFile = getResponse(fileUrl); assertThat(responseForFile).contains("mix:versionable"); } private String getResponse(String url) throws AuthenticationException, IOException, ClientProtocolException { HttpClient client = new DefaultHttpClient(); HttpGet request = getRequest(url); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); InputStream stream = entity.getContent(); BufferedInputStream bis = new BufferedInputStream(stream); byte[] buffer = new byte[8096]; StringBuilder builder = new StringBuilder(); while (bis.read(buffer) > 0) { builder.append(new String(buffer)); } return builder.toString(); } /** * @param url * @return * @throws AuthenticationException */ private HttpGet getRequest(String url) throws AuthenticationException { HttpGet request = new HttpGet(url); Credentials credentials = new UsernamePasswordCredentials("admin", "admin"); Header auth = new BasicScheme().authenticate(credentials, request); request.addHeader(auth); return request; } private Workspace getWorkspace() { String user = "admin"; String password = "admin"; Server server = new Server(url, user, password); Repository repository = new Repository("repository", server); Workspace workspace = new Workspace("default", repository); return workspace; } }