Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 511326 | Differences between
and this patch

Collapse All | Expand All

(-)a/bundles/org.eclipse.orion.server.servlets/src/org/eclipse/orion/internal/server/servlets/file/ServletFileStoreHandler.java (-2 / +118 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2010, 2016 IBM Corporation and others.
2
 * Copyright (c) 2010, 2017 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 10-18 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.orion.internal.server.servlets.file;
11
package org.eclipse.orion.internal.server.servlets.file;
12
12
13
import java.io.File;
13
import java.io.IOException;
14
import java.io.IOException;
15
import java.io.UnsupportedEncodingException;
14
import java.net.URI;
16
import java.net.URI;
15
import java.net.URISyntaxException;
17
import java.net.URISyntaxException;
18
import java.net.URLDecoder;
19
import java.util.HashMap;
16
20
17
import javax.servlet.ServletContext;
21
import javax.servlet.ServletContext;
18
import javax.servlet.ServletException;
22
import javax.servlet.ServletException;
Lines 28-33 Link Here
28
import org.eclipse.core.runtime.URIUtil;
32
import org.eclipse.core.runtime.URIUtil;
29
import org.eclipse.orion.internal.server.servlets.ServletResourceHandler;
33
import org.eclipse.orion.internal.server.servlets.ServletResourceHandler;
30
import org.eclipse.orion.server.core.EncodingUtils;
34
import org.eclipse.orion.server.core.EncodingUtils;
35
import org.eclipse.orion.server.core.IOUtilities;
36
import org.eclipse.orion.server.core.OrionConfiguration;
31
import org.eclipse.orion.server.core.ProtocolConstants;
37
import org.eclipse.orion.server.core.ProtocolConstants;
32
import org.eclipse.orion.server.core.ServerStatus;
38
import org.eclipse.orion.server.core.ServerStatus;
33
import org.eclipse.orion.server.servlets.OrionServlet;
39
import org.eclipse.orion.server.servlets.OrionServlet;
Lines 36-41 Link Here
36
import org.json.JSONException;
42
import org.json.JSONException;
37
import org.json.JSONObject;
43
import org.json.JSONObject;
38
import org.osgi.framework.Version;
44
import org.osgi.framework.Version;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
39
47
40
/**
48
/**
41
 * General responder for IFileStore. This class provides general support for serializing
49
 * General responder for IFileStore. This class provides general support for serializing
Lines 178-186 Link Here
178
			}
186
			}
179
			return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, 404, NLS.bind("File not found: {0}", EncodingUtils.encodeForHTML(request.getPathInfo())), null));
187
			return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, 404, NLS.bind("File not found: {0}", EncodingUtils.encodeForHTML(request.getPathInfo())), null));
180
		}
188
		}
181
		if (fileInfo.isDirectory())
189
		if("true".equals(IOUtilities.getQueryParameter(request, "project"))) {
190
			return getProject(request, response, file, fileInfo);
191
		}
192
		if (fileInfo.isDirectory()) {
182
			return handleDirectory(request, response, file);
193
			return handleDirectory(request, response, file);
194
		}
183
		return handleFile(request, response, file);
195
		return handleFile(request, response, file);
184
	}
196
	}
185
197
198
	/**
199
	 * Tries to find the enclosing project context for the given file information
200
	 * @param request The original request
201
	 * @param fileInfo The file information to start looking from
202
	 * @param names The map of file names that are considered "project-like" files
203
	 * @return The name of the project containing the given file information
204
	 * @throws ServletException 
205
	 * @since 14.0
206
	 */
207
	private boolean getProject(HttpServletRequest request, HttpServletResponse response, IFileStore file, IFileInfo fileInfo) throws ServletException {
208
		String n = IOUtilities.getQueryParameter(request, "names");
209
		HashMap<String, Boolean> names = new HashMap<String, Boolean>();
210
		names.put(".git", true);
211
		names.put("project.json", false);
212
		if(n != null) {
213
			try {
214
				String[] clientNames = URLDecoder.decode(n, "UTF-8").split(",");
215
				for (String _n : clientNames) {
216
					names.put(_n, false);
217
				}
218
			} catch(UnsupportedEncodingException use) {
219
				Logger logger = LoggerFactory.getLogger(ServletFileStoreHandler.class);
220
				logger.error("Failed to decode client project names: " + n);
221
			}
222
		}
223
		IFileStore project = findProject(request, file, fileInfo, names);
224
		if(project == null) {
225
			return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.WARNING, 204, NLS.bind("No project context for: {0}", EncodingUtils.encodeForHTML(fileInfo.getName())), null));
226
		}
227
		try {
228
			JSONObject result = toJSON(project, project.fetchInfo(), project.toURI());
229
			DirectoryHandlerV1.encodeChildren(project, project.toURI(), result, 0);
230
			OrionServlet.writeJSONResponse(request, response, result);
231
		} catch(CoreException ce) {
232
			Logger logger = LoggerFactory.getLogger(ServletFileStoreHandler.class);
233
			logger.error("Failed to encode children for project: " + project.getName());
234
		} catch (IOException ioe) {
235
			Logger logger = LoggerFactory.getLogger(ServletFileStoreHandler.class);
236
			logger.error("Failed to write response for project: " + project.getName());
237
		}
238
		return true;
239
	}
240
	
241
	/**
242
	 * @param fileInfo The originating fileInfo 
243
	 * @param file The originating file store
244
	 * @param request The original request
245
	 * @param names The map of names to consider project-like
246
	 * @return The {@link IFileStore} for the project or <code>null</code>
247
	 * @since 14.0
248
	 */
249
	private IFileStore findProject(HttpServletRequest request, IFileStore file, IFileInfo fileInfo, HashMap<String, Boolean> names) {
250
		IFileStore parent = file;
251
		try {
252
			String wsPath = getWorkspacePath();
253
			File parentFile = parent.toLocalFile(EFS.NONE, null);
254
			if (parentFile == null) {
255
				Logger logger = LoggerFactory.getLogger(ServletFileStoreHandler.class);
256
				logger.error("Unable to get the parent local file from: " + parent);
257
				return null;
258
			}
259
			while(parent != null && parentFile != null && parentFile.getAbsolutePath().startsWith(wsPath)) {
260
				IFileInfo[] children = parent.childInfos(EFS.NONE, null);
261
				for (IFileInfo childInfo : children) {
262
					if(childInfo.exists() && names.containsKey(childInfo.getName())) {
263
						if(childInfo.isDirectory() && names.get(childInfo.getName())) {
264
							return parent;
265
						} else if(!names.get(childInfo.getName())) {
266
							return parent;
267
						}
268
					}
269
				}
270
				parent = parent.getParent();
271
				parentFile = parent.toLocalFile(EFS.NONE, null);
272
			}
273
		} catch (CoreException ce) {
274
			Logger logger = LoggerFactory.getLogger(ServletFileStoreHandler.class);
275
			logger.error("Exception computing parent folder from: " + parent);
276
			return null;
277
		}
278
		return null;
279
	}
280
	
281
	/**
282
	 * Returns the workspace absolute path or <code>null</code>
283
	 * @return The absolute workspace path
284
	 * @since 14.0
285
	 */
286
	private String getWorkspacePath() {
287
		File workspaceRoot = null;
288
		try {
289
			workspaceRoot = OrionConfiguration.getRootLocation().toLocalFile(EFS.NONE, null);
290
			if (workspaceRoot == null) {
291
				Logger logger = LoggerFactory.getLogger(ServletFileStoreHandler.class);
292
				logger.error("Unable to get the root location from: " + OrionConfiguration.getRootLocation());
293
				return null;
294
			}
295
		} catch (CoreException e) {
296
			Logger logger = LoggerFactory.getLogger(ServletFileStoreHandler.class);
297
			logger.error("Unable to get the root location", e);
298
			return null;
299
		}
300
		return workspaceRoot.getAbsolutePath();
301
	}
186
}
302
}

Return to bug 511326