2
|
1 |
/*******************************************************************************
|
|
2 |
* Copyright (c) 2009 Symbian Foundation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Symbian Foundation - initial contribution.
|
|
11 |
* Contributors:
|
|
12 |
* Description:
|
|
13 |
* Overview:
|
|
14 |
* Details:
|
|
15 |
* Platforms/Drives/Compatibility:
|
|
16 |
* Assumptions/Requirement/Pre-requisites:
|
|
17 |
* Failures and causes:
|
|
18 |
*******************************************************************************/
|
|
19 |
package org.symbian.tools.wrttools.debug.internal.web;
|
|
20 |
|
|
21 |
import java.io.IOException;
|
|
22 |
import java.io.InputStream;
|
|
23 |
import java.io.UnsupportedEncodingException;
|
|
24 |
import java.net.MalformedURLException;
|
|
25 |
import java.net.URL;
|
|
26 |
import java.net.URLDecoder;
|
|
27 |
import java.net.URLEncoder;
|
|
28 |
import java.util.StringTokenizer;
|
|
29 |
|
|
30 |
import javax.servlet.ServletException;
|
|
31 |
import javax.servlet.http.HttpServlet;
|
|
32 |
import javax.servlet.http.HttpServletRequest;
|
|
33 |
import javax.servlet.http.HttpServletResponse;
|
|
34 |
|
|
35 |
import org.eclipse.core.resources.IFile;
|
|
36 |
import org.eclipse.core.resources.IResource;
|
|
37 |
import org.eclipse.core.resources.ResourcesPlugin;
|
|
38 |
import org.eclipse.core.runtime.CoreException;
|
|
39 |
import org.eclipse.core.runtime.IPath;
|
|
40 |
import org.eclipse.core.runtime.Path;
|
|
41 |
import org.symbian.tools.wrttools.debug.internal.Activator;
|
|
42 |
|
|
43 |
public class WorkspaceResourcesServlet extends HttpServlet {
|
|
44 |
private static final long serialVersionUID = -3217197074249607950L;
|
|
45 |
|
|
46 |
@Override
|
|
47 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
|
48 |
throws ServletException, IOException {
|
|
49 |
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(
|
|
50 |
new Path(req.getPathInfo()));
|
|
51 |
if (file.isAccessible()) {
|
|
52 |
InputStream contents = null;
|
|
53 |
try {
|
|
54 |
contents = file.getContents();
|
|
55 |
byte[] buf = new byte[4048];
|
|
56 |
int i;
|
|
57 |
while ((i = contents.read(buf)) >= 0) {
|
|
58 |
resp.getOutputStream().write(buf, 0, i);
|
|
59 |
}
|
|
60 |
} catch (CoreException e) {
|
|
61 |
Activator.log(e);
|
|
62 |
} finally {
|
|
63 |
if (contents != null) {
|
|
64 |
contents.close();
|
|
65 |
}
|
|
66 |
}
|
|
67 |
} else {
|
|
68 |
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
public static String getHttpUrl(IResource file) {
|
|
73 |
try {
|
|
74 |
String path = WebappManager.WORKSPACE_RESOURCES_CONTEXT
|
|
75 |
+ (file != null ? encode(file.getFullPath()) : "/");
|
|
76 |
URL url = new URL("http", WebappManager.getHost(), WebappManager
|
|
77 |
.getPort(), path);
|
|
78 |
return url.toString();
|
|
79 |
} catch (MalformedURLException e) {
|
|
80 |
return file.getLocationURI().toString();
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
private static String encode(IPath fullPath) {
|
|
85 |
try {
|
|
86 |
StringBuffer result = new StringBuffer();
|
|
87 |
String[] segments = fullPath.segments();
|
|
88 |
for (int i = 0; i < segments.length; i++) {
|
|
89 |
String string = segments[i];
|
|
90 |
result.append("/");
|
|
91 |
// java.net.URLEncoder encodes " " as "+" while Chrome needs "%20"
|
|
92 |
StringTokenizer tokenizer = new StringTokenizer(string, " ", false);
|
|
93 |
while (tokenizer.hasMoreElements()) {
|
|
94 |
result.append(URLEncoder.encode(tokenizer.nextToken(),
|
|
95 |
"UTF-8"));
|
|
96 |
if (tokenizer.hasMoreTokens()) {
|
|
97 |
result.append("%20");
|
|
98 |
}
|
|
99 |
}
|
|
100 |
}
|
|
101 |
return result.toString();
|
|
102 |
} catch (UnsupportedEncodingException e) {
|
|
103 |
// Something is horribly wrong - JRE doesn't have UTF8?
|
|
104 |
throw new RuntimeException(e);
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
public static IFile getFileFromUrl(String name) {
|
|
109 |
try {
|
|
110 |
String root = getHttpUrl(null);
|
|
111 |
IFile file = null;
|
|
112 |
if (name.startsWith(root)) {
|
|
113 |
String fileName = name.substring(root.length());
|
|
114 |
fileName = URLDecoder.decode(fileName, "UTF-8");
|
|
115 |
final IPath path = new Path(fileName);
|
|
116 |
file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
|
|
117 |
if (!file.isAccessible()) {
|
|
118 |
return null;
|
|
119 |
}
|
|
120 |
}
|
|
121 |
return file;
|
|
122 |
} catch (UnsupportedEncodingException e) {
|
|
123 |
throw new RuntimeException(e);
|
|
124 |
}
|
|
125 |
}
|
|
126 |
}
|