Bug 508538 - downloadChangePackageFragment rejects valid users

Change-Id: I177b0587cb1c63b60cdda332e61d2462adf7b3f8
Signed-off-by: Johannes Faltermeier <jfaltermeier@eclipsesource.com>
diff --git a/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/Messages.java b/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/Messages.java
index ee286bd..70a256c 100644
--- a/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/Messages.java
+++ b/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/Messages.java
@@ -26,6 +26,7 @@
 	public static String XmlRpcClientManager_DownloadOfFragmentFailed;
 	public static String XmlRpcClientManager_UploadChangePackageFragmentCallFailed;
 	public static String XmlRpcClientManager_Malformed_URL_Or_Port;
+	public static String XmlRpcClientManager_NoValidProjectId;
 	public static String XmlRpcClientManager_NoValidSessionAndOrProjectId;
 	public static String XmlRpcClientManager_NoValidSessionId;
 	public static String XmlRpcClientManager_SaveChangePackageFailed;
diff --git a/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/XmlRpcClientManager.java b/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/XmlRpcClientManager.java
index 60da331..f1cedd1 100644
--- a/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/XmlRpcClientManager.java
+++ b/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/XmlRpcClientManager.java
@@ -182,7 +182,10 @@
 
 		try {
 			final T result = (T) client.execute(serverInterface + "." + methodName, adjustedParams); //$NON-NLS-1$
-			return adjustResult(ESCollections.find(params, SessionId.class), result);
+			return adjustResult(
+				ESCollections.find(params, SessionId.class),
+				ESCollections.find(params, ProjectId.class),
+				result);
 
 		} catch (final XmlRpcException e) {
 			if (e.getCause() instanceof ESException) {
@@ -225,33 +228,40 @@
 	}
 
 	@SuppressWarnings({ "rawtypes", "unchecked" })
-	private <T> T adjustResult(final Optional<SessionId> maybeSessionId, final T result) throws ESException {
+	private <T> T adjustResult(
+		final Optional<SessionId> maybeSessionId,
+		final Optional<ProjectId> maybeProjectId,
+		final T result) throws ESException {
 		if (result instanceof Object[]) {
 			final Object[] objects = (Object[]) result;
 			for (int i = 0; i < objects.length; i++) {
 				final Object item = objects[i];
-				objects[i] = adjustResult(maybeSessionId, item);
+				objects[i] = adjustResult(maybeSessionId, maybeProjectId, item);
 			}
 			return (T) objects;
 		} else if (result instanceof List) {
 			final List l = (List) result;
 			for (int i = 0; i < l.size(); i++) {
-				l.set(i, adjustResult(maybeSessionId, result));
+				l.set(i, adjustResult(maybeSessionId, maybeProjectId, result));
 			}
 		} else if (result instanceof ChangePackageProxy) {
-			return (T) downloadAndResolveChangePackage((ChangePackageProxy) result, maybeSessionId);
+			return (T) downloadAndResolveChangePackage((ChangePackageProxy) result, maybeSessionId, maybeProjectId);
 		}
 
 		return result;
 	}
 
 	private AbstractChangePackage downloadAndResolveChangePackage(final ChangePackageProxy proxy,
-		final Optional<SessionId> maybeSession) throws ESException {
+		final Optional<SessionId> maybeSession, Optional<ProjectId> maybeProjectId) throws ESException {
 
 		if (!maybeSession.isPresent()) {
 			throw new ESException(Messages.XmlRpcClientManager_NoValidSessionId);
 		}
 
+		if (!maybeProjectId.isPresent()) {
+			throw new ESException(Messages.XmlRpcClientManager_NoValidProjectId);
+		}
+
 		int fragmentIndex = 0;
 		final FileBasedChangePackage changePackage = VersioningFactory.eINSTANCE
 			.createFileBasedChangePackage();
@@ -265,6 +275,7 @@
 			do {
 				envelope = executeCall("downloadChangePackageFragment", ChangePackageEnvelope.class, new Object[] { //$NON-NLS-1$
 					maybeSession.get(),
+					maybeProjectId.get(),
 					proxy.getId(),
 					fragmentIndex
 				});
diff --git a/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/XmlRpcConnectionManager.java b/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/XmlRpcConnectionManager.java
index 463a370..1fdf753 100644
--- a/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/XmlRpcConnectionManager.java
+++ b/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/XmlRpcConnectionManager.java
@@ -329,17 +329,20 @@
 	}
 
 	/**
+	 *
 	 * {@inheritDoc}
 	 *
 	 * @see org.eclipse.emf.emfstore.internal.server.EMFStore#downloadChangePackageFragment(org.eclipse.emf.emfstore.internal.server.model.SessionId,
-	 *      java.lang.String, int)
+	 *      org.eclipse.emf.emfstore.internal.server.model.ProjectId, java.lang.String, int)
 	 */
-	public ChangePackageEnvelope downloadChangePackageFragment(SessionId sessionId, String proxyId, int fragmentIndex)
+	public ChangePackageEnvelope downloadChangePackageFragment(SessionId sessionId, ProjectId projectId, String proxyId,
+		int fragmentIndex)
 		throws ESException {
 		return getConnectionProxy(sessionId)
 			.callWithResult("downloadChangePackageFragment", //$NON-NLS-1$
 				ChangePackageEnvelope.class,
 				sessionId,
+				projectId,
 				proxyId,
 				fragmentIndex);
 	}
diff --git a/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/messages.properties b/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/messages.properties
index 14e365c..e20d259 100644
--- a/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/messages.properties
+++ b/bundles/org.eclipse.emf.emfstore.client/src/org/eclipse/emf/emfstore/internal/client/model/connectionmanager/xmlrpc/messages.properties
@@ -2,6 +2,7 @@
 XmlRpcClientManager_DownloadOfFragmentFailed=Download of change package fragment failed
 XmlRpcClientManager_UploadChangePackageFragmentCallFailed=Could not create change package fragment.
 XmlRpcClientManager_Malformed_URL_Or_Port=Malformed URL or Port
+XmlRpcClientManager_NoValidProjectId=Cannot resolve a change package proxy because no valid project ID present.
 XmlRpcClientManager_NoValidSessionAndOrProjectId=Cannot fragment change package without valid session and project ID.
 XmlRpcClientManager_NoValidSessionId=Cannot resolve a change package proxy because no valid session ID present.
 XmlRpcClientManager_SaveChangePackageFailed=Could not save change package.
diff --git a/bundles/org.eclipse.emf.emfstore.server/src/org/eclipse/emf/emfstore/internal/server/EMFStore.java b/bundles/org.eclipse.emf.emfstore.server/src/org/eclipse/emf/emfstore/internal/server/EMFStore.java
index 889ba1e..9137dc6 100644
--- a/bundles/org.eclipse.emf.emfstore.server/src/org/eclipse/emf/emfstore/internal/server/EMFStore.java
+++ b/bundles/org.eclipse.emf.emfstore.server/src/org/eclipse/emf/emfstore/internal/server/EMFStore.java
@@ -146,6 +146,8 @@
 	 *
 	 * @param sessionId
 	 *            the {@link SessionId} for authentication purposes
+	 * @param projectId
+	 *            the {@link ProjectId} for access control purposes
 	 * @param proxyId
 	 *            the ID of the change package proxy that is used to identify available fragments
 	 * @param fragmentIndex
@@ -153,8 +155,9 @@
 	 * @return a {@link ChangePackageEnvelope} containing the requested change package fragment
 	 * @throws ESException in case the fragment could not be fetched
 	 */
-	ChangePackageEnvelope downloadChangePackageFragment(SessionId sessionId, String proxyId, int fragmentIndex)
-		throws ESException;
+	ChangePackageEnvelope downloadChangePackageFragment(SessionId sessionId, ProjectId projectId, String proxyId,
+		int fragmentIndex)
+			throws ESException;
 
 	/**
 	 * Resolve a version specified to a primary version specifier.
@@ -192,7 +195,7 @@
 	 */
 	List<AbstractChangePackage> getChanges(SessionId sessionId, ProjectId projectId, VersionSpec source,
 		VersionSpec target)
-		throws ESException;
+			throws ESException;
 
 	/**
 	 * Lista all branches of the given project.
diff --git a/bundles/org.eclipse.emf.emfstore.server/src/org/eclipse/emf/emfstore/internal/server/connection/xmlrpc/XmlRpcEmfStoreImpl.java b/bundles/org.eclipse.emf.emfstore.server/src/org/eclipse/emf/emfstore/internal/server/connection/xmlrpc/XmlRpcEmfStoreImpl.java
index 9292013..f4dc2fe 100644
--- a/bundles/org.eclipse.emf.emfstore.server/src/org/eclipse/emf/emfstore/internal/server/connection/xmlrpc/XmlRpcEmfStoreImpl.java
+++ b/bundles/org.eclipse.emf.emfstore.server/src/org/eclipse/emf/emfstore/internal/server/connection/xmlrpc/XmlRpcEmfStoreImpl.java
@@ -153,7 +153,7 @@
 	 */
 	public FileChunk downloadFileChunk(SessionId sessionId, ProjectId projectId,
 		FileTransferInformation fileInformation)
-		throws ESException {
+			throws ESException {
 		try {
 			return getEmfStore().downloadFileChunk(sessionId, projectId, fileInformation);
 		} catch (final FileNotOnServerException ex) {
@@ -308,15 +308,16 @@
 	}
 
 	/**
-	 *
+	 * 
 	 * {@inheritDoc}
-	 *
+	 * 
 	 * @see org.eclipse.emf.emfstore.internal.server.EMFStore#downloadChangePackageFragment(org.eclipse.emf.emfstore.internal.server.model.SessionId,
-	 *      java.lang.String, int)
+	 *      org.eclipse.emf.emfstore.internal.server.model.ProjectId, java.lang.String, int)
 	 */
-	public ChangePackageEnvelope downloadChangePackageFragment(SessionId sessionId, String proxyId, int fragmentIndex)
-		throws ESException {
-		return getEmfStore().downloadChangePackageFragment(sessionId, proxyId, fragmentIndex);
+	public ChangePackageEnvelope downloadChangePackageFragment(SessionId sessionId, ProjectId projectId, String proxyId,
+		int fragmentIndex)
+			throws ESException {
+		return getEmfStore().downloadChangePackageFragment(sessionId, projectId, proxyId, fragmentIndex);
 	}
 
 }
diff --git a/bundles/org.eclipse.emf.emfstore.server/src/org/eclipse/emf/emfstore/internal/server/core/subinterfaces/VersionSubInterfaceImpl.java b/bundles/org.eclipse.emf.emfstore.server/src/org/eclipse/emf/emfstore/internal/server/core/subinterfaces/VersionSubInterfaceImpl.java
index 51b187e..02b6002 100644
--- a/bundles/org.eclipse.emf.emfstore.server/src/org/eclipse/emf/emfstore/internal/server/core/subinterfaces/VersionSubInterfaceImpl.java
+++ b/bundles/org.eclipse.emf.emfstore.server/src/org/eclipse/emf/emfstore/internal/server/core/subinterfaces/VersionSubInterfaceImpl.java
@@ -304,6 +304,7 @@
 	 *
 	 * @param sessionId
 	 *            the {@link SessionId} representing the requesting user
+	 * @param projectId the {@link ProjectId} of the associated Project
 	 * @param proxyId
 	 *            the ID that identifies the list of stored fragments
 	 * @param fragmentIndex
@@ -312,7 +313,8 @@
 	 * @throws ESException in case the mandatory session adapter is missing
 	 */
 	@ESMethod(MethodId.DOWNLOADCHANGEPACKAGEFRAGMENT)
-	public ChangePackageEnvelope downloadChangePackageFragment(SessionId sessionId, String proxyId, int fragmentIndex)
+	public ChangePackageEnvelope downloadChangePackageFragment(SessionId sessionId, ProjectId projectId, String proxyId,
+		int fragmentIndex)
 		throws ESException {
 
 		final ESSessionId resolvedSession = getAccessControl().getSessions().resolveSessionById(sessionId.getId());
@@ -396,7 +398,8 @@
 			sourceVersion, logMessage, user);
 
 		ModelUtil.logProjectDetails("Creating version on server... done", user.getName(), //$NON-NLS-1$
-			projectHistory.getProjectName(), projectHistory.getProjectId().getId(), targetBranch != null ? targetBranch.getBranch() : null,
+			projectHistory.getProjectName(), projectHistory.getProjectId().getId(),
+			targetBranch != null ? targetBranch.getBranch() : null,
 			baseVersionSpec.getIdentifier());
 
 		return result;
diff --git a/tests/org.eclipse.emf.emfstore.test.common/src/org/eclipse/emf/emfstore/client/test/common/mocks/ConnectionMock.java b/tests/org.eclipse.emf.emfstore.test.common/src/org/eclipse/emf/emfstore/client/test/common/mocks/ConnectionMock.java
index 8057880..e7b0547 100644
--- a/tests/org.eclipse.emf.emfstore.test.common/src/org/eclipse/emf/emfstore/client/test/common/mocks/ConnectionMock.java
+++ b/tests/org.eclipse.emf.emfstore.test.common/src/org/eclipse/emf/emfstore/client/test/common/mocks/ConnectionMock.java
@@ -337,18 +337,20 @@
 	}
 
 	/**
+	 *
 	 * {@inheritDoc}
 	 *
 	 * @see org.eclipse.emf.emfstore.internal.server.EMFStore#downloadChangePackageFragment(org.eclipse.emf.emfstore.internal.server.model.SessionId,
-	 *      java.lang.String, int)
+	 *      org.eclipse.emf.emfstore.internal.server.model.ProjectId, java.lang.String, int)
 	 */
-	public ChangePackageEnvelope downloadChangePackageFragment(final SessionId sessionId, final String proxyId,
-		final int fragmentIndex)
+	public ChangePackageEnvelope downloadChangePackageFragment(final SessionId sessionId, ProjectId projectId,
+		final String proxyId, final int fragmentIndex)
 		throws ESException {
 		final SessionId clonedSessionId = checkSessionId(sessionId);
 		return ModelUtil.clone(
 			emfStore.downloadChangePackageFragment(
 				clonedSessionId,
+				projectId,
 				proxyId,
 				fragmentIndex));
 	}