From 2dab6047743f831f68aa1618e70259be77c0ca8e Mon Sep 17 00:00:00 2001 From: Dragos Corlatescu Date: Wed, 12 Feb 2014 10:34:46 +0200 Subject: [PATCH] Bug 416119 POST requests now updates attributes of the file/directory. --- .classpath | 986 ++++++++++++++++++++ .../server/servlets/file/DirectoryHandlerV1.java | 38 +- .../org.eclipse.orion.target.target | 993 +++++++++++---------- .../server/tests/servlets/files/CoreFilesTest.java | 77 ++ .../tests/servlets/files/FileSystemTest.java | 68 +- 5 files changed, 1653 insertions(+), 509 deletions(-) create mode 100644 .classpath diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..3bd8bb6 --- /dev/null +++ b/.classpath @@ -0,0 +1,986 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/bundles/org.eclipse.orion.server.servlets/src/org/eclipse/orion/internal/server/servlets/file/DirectoryHandlerV1.java b/bundles/org.eclipse.orion.server.servlets/src/org/eclipse/orion/internal/server/servlets/file/DirectoryHandlerV1.java index c3e5eb5..e2fbfd5 100644 --- a/bundles/org.eclipse.orion.server.servlets/src/org/eclipse/orion/internal/server/servlets/file/DirectoryHandlerV1.java +++ b/bundles/org.eclipse.orion.server.servlets/src/org/eclipse/orion/internal/server/servlets/file/DirectoryHandlerV1.java @@ -12,12 +12,19 @@ package org.eclipse.orion.internal.server.servlets.file; import java.io.IOException; import java.io.UnsupportedEncodingException; -import java.net.*; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLDecoder; + import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.eclipse.core.filesystem.*; -import org.eclipse.core.runtime.*; + +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileInfo; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.URIUtil; import org.eclipse.orion.internal.server.servlets.ProtocolConstants; import org.eclipse.orion.internal.server.servlets.ServletResourceHandler; @@ -25,7 +32,9 @@ import org.eclipse.orion.server.core.LogHelper; import org.eclipse.orion.server.core.ServerStatus; import org.eclipse.orion.server.servlets.OrionServlet; import org.eclipse.osgi.util.NLS; -import org.json.*; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; /** * Handles HTTP requests against directories for eclipse web protocol version @@ -114,17 +123,35 @@ public class DirectoryHandlerV1 extends ServletResourceHandler { * Performs the actual modification corresponding to a POST request. All preconditions * are assumed to be satisfied. * @return true if the operation was successful, and false otherwise. + * @throws JSONException */ - private boolean performPost(HttpServletRequest request, HttpServletResponse response, JSONObject requestObject, IFileStore toCreate, int options) throws CoreException, IOException, ServletException { + private boolean performPost(HttpServletRequest request, HttpServletResponse response, JSONObject requestObject, IFileStore toCreate, int options) throws CoreException, IOException, ServletException, JSONException { boolean isCopy = (options & CREATE_COPY) != 0; boolean isMove = (options & CREATE_MOVE) != 0; try { if (isCopy || isMove) return performCopyMove(request, response, requestObject, toCreate, isCopy, options); + + IFileInfo info = toCreate.fetchInfo(EFS.NONE, null); + if (info.exists() && (options & CREATE_NO_OVERWRITE) == 0) { + if (requestObject.has("Attributes")) { + JSONObject attributes = requestObject.getJSONObject("Attributes"); + if (attributes.has("ReadOnly")) { + info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, attributes.getBoolean("ReadOnly")); + } + if (attributes.has("Executable")) { + info.setAttribute(EFS.ATTRIBUTE_EXECUTABLE, attributes.getBoolean("Executable")); + } + toCreate.putInfo(info, EFS.SET_ATTRIBUTES, null); + return true; + } + } + if (requestObject.optBoolean(ProtocolConstants.KEY_DIRECTORY)) toCreate.mkdir(EFS.NONE, null); else toCreate.openOutputStream(EFS.NONE, null).close(); + } catch (CoreException e) { IStatus status = e.getStatus(); if (status != null && status.getCode() == EFS.ERROR_WRITE) { @@ -140,6 +167,7 @@ public class DirectoryHandlerV1 extends ServletResourceHandler { /** * Perform a copy or move as specified by the request. * @return true if the operation was successful, and false otherwise. + * @throws JSONException */ private boolean performCopyMove(HttpServletRequest request, HttpServletResponse response, JSONObject requestObject, IFileStore toCreate, boolean isCopy, int options) throws ServletException, CoreException { String locationString = requestObject.optString(ProtocolConstants.KEY_LOCATION, null); diff --git a/releng/org.eclipse.orion.target/org.eclipse.orion.target.target b/releng/org.eclipse.orion.target/org.eclipse.orion.target.target index aad771e..5dba15f 100644 --- a/releng/org.eclipse.orion.target/org.eclipse.orion.target.target +++ b/releng/org.eclipse.orion.target/org.eclipse.orion.target.target @@ -1,496 +1,497 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/files/CoreFilesTest.java b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/files/CoreFilesTest.java index bb3c84a..f5e1a0f 100644 --- a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/files/CoreFilesTest.java +++ b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/files/CoreFilesTest.java @@ -177,6 +177,32 @@ public class CoreFilesTest extends FileSystemTest { } @Test + public void testChangeDirectoryAttributesByPost() throws CoreException, IOException, SAXException, JSONException { + String directoryPath = "sample/directory/path" + System.currentTimeMillis(); + createDirectory(directoryPath); + + String dirName = "testdir"; + webConversation.setExceptionsThrownOnErrorStatus(false); + + WebRequest requestCreate = getPostFilesRequest(directoryPath, getNewDirJSON(dirName).toString(), dirName); + WebResponse responseCreate = webConversation.getResponse(requestCreate); + + assertEquals(HttpURLConnection.HTTP_CREATED, responseCreate.getResponseCode()); + assertTrue("Create directory response was OK, but the directory does not exist", checkDirectoryExists(directoryPath + "/" + dirName)); + assertEquals("Response should contain directory metadata in JSON, but was " + responseCreate.getText(), "application/json", responseCreate.getContentType()); + JSONObject responseCreateObject = new JSONObject(responseCreate.getText()); + assertNotNull("No directory information in response", responseCreateObject); + checkDirectoryMetadata(responseCreateObject, dirName, null, null, null, null, null); + + WebRequest requestUpdateAttributes = getPostFilesRequest(directoryPath, getNewDirWithAttrJSON(dirName).toString(), dirName); + WebResponse responseUpdateAttributes = webConversation.getResponse(requestUpdateAttributes); + + JSONObject responseUpdateAttributesObject = new JSONObject(responseUpdateAttributes.getText()); + assertNotNull("No directory information in response", responseUpdateAttributesObject); + checkDirectoryMetadata(responseUpdateAttributesObject, dirName, null, null, true, false, null); + } + + @Test public void testCreateEmptyFile() throws CoreException, IOException, SAXException, JSONException { String directoryPath = "sample/directory/path" + System.currentTimeMillis(); createDirectory(directoryPath); @@ -203,6 +229,57 @@ public class CoreFilesTest extends FileSystemTest { checkFileMetadata(responseObject, fileName, null, null, null, null, null, null, null, null); } + @Test + public void testChangeFileAttributesByPost() throws CoreException, IOException, SAXException, JSONException { + String directoryPath = "sample/directory/path" + System.currentTimeMillis(); + createDirectory(directoryPath); + String fileName = "testfile.txt"; + + WebRequest request = getPostFilesRequest(directoryPath, getNewFileJSON(fileName).toString(), fileName); + WebResponse response = webConversation.getResponse(request); + + assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode()); + assertTrue("Create file response was OK, but the file does not exist", checkFileExists(directoryPath + "/" + fileName)); + assertEquals("Response should contain file metadata in JSON, but was " + response.getText(), "application/json", response.getContentType()); + JSONObject responseObject = new JSONObject(response.getText()); + assertNotNull("No file information in response", responseObject); + checkFileMetadata(responseObject, fileName, null, null, null, null, null, null, null, null); + + //should be able to perform GET on location header to obtain metadata + String location = response.getHeaderField("Location"); + request = getGetRequest(location + "?parts=meta"); + response = webConversation.getResource(request); + assertNotNull(location); + assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); + responseObject = new JSONObject(response.getText()); + assertNotNull("No direcory information in response", responseObject); + checkFileMetadata(responseObject, fileName, null, null, null, null, null, null, null, null); + + //create an empty file with different attributes and update the current one using post + request = getPostFilesRequest(directoryPath, getNewFileWithAttrJSON(fileName).toString(), fileName); + response = webConversation.getResponse(request); + responseObject = new JSONObject(response.getText()); + checkFileMetadata(responseObject, fileName, null, null, null, null, null, true, false, null); + + assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); + assertTrue("Create file response was OK, but the file does not exist", checkFileExists(directoryPath + "/" + fileName)); + assertEquals("Response should contain file metadata in JSON, but was " + response.getText(), "application/json", response.getContentType()); + responseObject = new JSONObject(response.getText()); + assertNotNull("No file information in response", responseObject); + checkFileMetadata(responseObject, fileName, null, null, null, null, null, true, false, null); + + //should be able to perform GET on location header to obtain metadata + location = response.getHeaderField("Location"); + request = getGetRequest(location + "?parts=meta"); + response = webConversation.getResource(request); + assertNotNull(location); + assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); + responseObject = new JSONObject(response.getText()); + assertNotNull("No direcory information in response", responseObject); + checkFileMetadata(responseObject, fileName, null, null, null, null, null, true, false, null); + + } + /** * Tests creating a file whose name is an encoded URL. */ diff --git a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/files/FileSystemTest.java b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/files/FileSystemTest.java index 85b2e5d..3664fa4 100644 --- a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/files/FileSystemTest.java +++ b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/files/FileSystemTest.java @@ -16,13 +16,26 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.meterware.httpunit.*; -import java.io.*; -import java.net.*; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; -import org.eclipse.core.filesystem.*; -import org.eclipse.core.runtime.*; + +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileInfo; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.URIUtil; import org.eclipse.orion.internal.server.core.IOUtilities; import org.eclipse.orion.internal.server.core.metastore.SimpleMetaStore; @@ -30,13 +43,25 @@ import org.eclipse.orion.internal.server.core.metastore.SimpleMetaStoreUtil; import org.eclipse.orion.internal.server.servlets.ProtocolConstants; import org.eclipse.orion.server.core.LogHelper; import org.eclipse.orion.server.core.OrionConfiguration; -import org.eclipse.orion.server.core.metastore.*; +import org.eclipse.orion.server.core.metastore.IMetaStore; +import org.eclipse.orion.server.core.metastore.ProjectInfo; +import org.eclipse.orion.server.core.metastore.UserInfo; +import org.eclipse.orion.server.core.metastore.WorkspaceInfo; import org.eclipse.orion.server.tests.AbstractServerTest; import org.eclipse.orion.server.tests.ServerTestsActivator; import org.eclipse.orion.server.tests.servlets.internal.DeleteMethodWebRequest; -import org.json.*; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import org.xml.sax.SAXException; +import com.meterware.httpunit.GetMethodWebRequest; +import com.meterware.httpunit.PostMethodWebRequest; +import com.meterware.httpunit.PutMethodWebRequest; +import com.meterware.httpunit.WebConversation; +import com.meterware.httpunit.WebRequest; +import com.meterware.httpunit.WebResponse; + /** * Common base class for file system tests. */ @@ -258,7 +283,7 @@ public abstract class FileSystemTest extends AbstractServerTest { JSONObject attributes = dirObject.getJSONObject(ProtocolConstants.KEY_ATTRIBUTES); assertNotNull("Expected Attributes section in directory metadata", attributes); if (isReadonly != null) { - assertEquals("Ibvalid directory readonly attribute", isReadonly, attributes.getBoolean(ProtocolConstants.KEY_ATTRIBUTE_READ_ONLY)); + assertEquals("Invalid directory readonly attribute", isReadonly, attributes.getBoolean(ProtocolConstants.KEY_ATTRIBUTE_READ_ONLY)); } if (isExecutable != null) { assertEquals("Invalid directory executable attribute", isExecutable, attributes.getBoolean(ProtocolConstants.KEY_ATTRIBUTE_EXECUTABLE)); @@ -346,6 +371,19 @@ public abstract class FileSystemTest extends AbstractServerTest { return request; } + protected JSONObject getNewDirWithAttrJSON(String dirName) throws JSONException { + JSONObject attr = new JSONObject(); + attr.put(ProtocolConstants.KEY_ATTRIBUTE_READ_ONLY, true); + attr.put(ProtocolConstants.KEY_ATTRIBUTE_EXECUTABLE, false); + attr.put(ProtocolConstants.KEY_ATTRIBUTE_SYMLINK, true); + + JSONObject json = new JSONObject(); + json.put(ProtocolConstants.KEY_NAME, dirName); + json.put(ProtocolConstants.KEY_DIRECTORY, true); + json.put(ProtocolConstants.KEY_ATTRIBUTES, attr); + return json; + } + protected JSONObject getNewDirJSON(String dirName) throws JSONException { JSONObject json = new JSONObject(); json.put(ProtocolConstants.KEY_NAME, dirName); @@ -353,6 +391,20 @@ public abstract class FileSystemTest extends AbstractServerTest { return json; } + protected JSONObject getNewFileWithAttrJSON(String fileName) throws JSONException { + JSONObject attr = new JSONObject(); + attr.put(ProtocolConstants.KEY_ATTRIBUTE_READ_ONLY, true); + attr.put(ProtocolConstants.KEY_ATTRIBUTE_EXECUTABLE, false); + attr.put(ProtocolConstants.KEY_ATTRIBUTE_SYMLINK, true); + + JSONObject json = new JSONObject(); + json.put(ProtocolConstants.KEY_DIRECTORY, false); + json.put("Charset", "UTF-8"); + json.put("ContentType", "text/plain"); + json.put(ProtocolConstants.KEY_ATTRIBUTES, attr); + return json; + } + protected JSONObject getNewFileJSON(String dirName) throws JSONException { JSONObject json = new JSONObject(); if (dirName != null) -- 1.8.1.2