16 * Assumptions/Requirement/Pre-requisites: |
16 * Assumptions/Requirement/Pre-requisites: |
17 * Failures and causes: |
17 * Failures and causes: |
18 *******************************************************************************/ |
18 *******************************************************************************/ |
19 package org.symbian.tools.wrttools.previewer.http; |
19 package org.symbian.tools.wrttools.previewer.http; |
20 |
20 |
|
21 import java.io.File; |
21 import java.io.IOException; |
22 import java.io.IOException; |
22 import java.io.InputStream; |
23 import java.io.InputStream; |
23 import java.io.OutputStream; |
24 import java.io.OutputStream; |
24 import java.io.UnsupportedEncodingException; |
25 import java.io.UnsupportedEncodingException; |
25 import java.net.MalformedURLException; |
26 import java.net.MalformedURLException; |
41 |
42 |
42 import org.eclipse.core.resources.IFile; |
43 import org.eclipse.core.resources.IFile; |
43 import org.eclipse.core.resources.IProject; |
44 import org.eclipse.core.resources.IProject; |
44 import org.eclipse.core.resources.ResourcesPlugin; |
45 import org.eclipse.core.resources.ResourcesPlugin; |
45 import org.eclipse.core.runtime.CoreException; |
46 import org.eclipse.core.runtime.CoreException; |
|
47 import org.eclipse.core.runtime.FileLocator; |
46 import org.eclipse.core.runtime.IPath; |
48 import org.eclipse.core.runtime.IPath; |
47 import org.eclipse.core.runtime.Path; |
49 import org.eclipse.core.runtime.Path; |
48 import org.json.simple.JSONObject; |
50 import org.json.simple.JSONObject; |
49 import org.json.simple.parser.JSONParser; |
51 import org.json.simple.parser.JSONParser; |
50 import org.json.simple.parser.ParseException; |
52 import org.json.simple.parser.ParseException; |
51 import org.symbian.tools.tmw.previewer.core.IApplicationLayoutProvider; |
53 import org.symbian.tools.tmw.previewer.core.IApplicationLayoutProvider; |
52 import org.symbian.tools.wrttools.previewer.PreviewerException; |
54 import org.symbian.tools.wrttools.previewer.PreviewerException; |
53 import org.symbian.tools.wrttools.previewer.PreviewerPlugin; |
55 import org.symbian.tools.wrttools.previewer.PreviewerPlugin; |
54 import org.symbian.tools.wrttools.previewer.http.handlers.DebuggerResourceProvider; |
56 import org.symbian.tools.wrttools.previewer.http.handlers.DebuggerResourceProvider; |
|
57 import org.symbian.tools.wrttools.previewer.http.handlers.PreviewerStaticResourceProvider; |
55 import org.symbian.tools.wrttools.previewer.http.handlers.Providers; |
58 import org.symbian.tools.wrttools.previewer.http.handlers.Providers; |
56 |
59 |
57 public class WorkspaceResourcesServlet extends HttpServlet { |
60 public class WorkspaceResourcesServlet extends HttpServlet { |
58 private static final Map<String, String> EXTENSION_CONTENT_TYPE = new TreeMap<String, String>(); |
61 private static final Map<String, String> EXTENSION_CONTENT_TYPE = new TreeMap<String, String>(); |
59 |
62 |
95 throw new RuntimeException(e); |
98 throw new RuntimeException(e); |
96 } |
99 } |
97 } |
100 } |
98 |
101 |
99 public static IFile getFileFromUrl(String name) { |
102 public static IFile getFileFromUrl(String name) { |
|
103 final IPath path = getResourcePath(name); |
|
104 if (path != null) { |
|
105 final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.segment(0)); |
|
106 final IApplicationLayoutProvider provider = PreviewerPlugin.getExtensionsManager().getLayoutProvider( |
|
107 project); |
|
108 if (provider != null) { |
|
109 try { |
|
110 return provider.getWorkspaceFile(project, path.removeFirstSegments(1)); |
|
111 } catch (CoreException e) { |
|
112 PreviewerPlugin.log(e); |
|
113 } |
|
114 } |
|
115 } |
|
116 return null; |
|
117 } |
|
118 |
|
119 private static IPath getResourcePath(String name) { |
|
120 IPath p = null; |
100 try { |
121 try { |
101 String root = getHttpUrl(null); |
122 String root = getHttpUrl(null); |
102 if (name != null && name.startsWith(root)) { |
123 if (name != null && name.startsWith(root)) { |
103 final String fileName = URLDecoder.decode(name.substring(root.length()), "UTF-8"); |
124 final String fileName = URLDecoder.decode(name.substring(root.length()), "UTF-8"); |
104 final IPath path = new Path(fileName); |
125 final IPath path = new Path(fileName); |
105 if (path.segmentCount() > 1) { |
126 if (path.segmentCount() > 1) { |
106 final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.segment(0)); |
127 p = path; |
107 final IApplicationLayoutProvider provider = PreviewerPlugin.getExtensionsManager() |
|
108 .getLayoutProvider(project); |
|
109 if (provider != null) { |
|
110 return provider.getWorkspaceFile(project, path.removeFirstSegments(1)); |
|
111 } |
|
112 } |
128 } |
113 } |
129 } |
114 } catch (UnsupportedEncodingException e) { |
130 } catch (UnsupportedEncodingException e) { |
115 throw new RuntimeException(e); |
131 throw new RuntimeException(e); |
116 } catch (CoreException e) { |
132 } |
117 PreviewerPlugin.log(e); |
133 return p; |
118 } |
|
119 return null; |
|
120 } |
134 } |
121 |
135 |
122 public static String getHttpUrl(IFile file) { |
136 public static String getHttpUrl(IFile file) { |
123 IPath p = new Path("/"); |
137 IPath p = new Path("/"); |
124 if (file != null) { |
138 if (file != null) { |
140 if (extension != null) { |
154 if (extension != null) { |
141 return EXTENSION_CONTENT_TYPE.get(extension.toLowerCase()); |
155 return EXTENSION_CONTENT_TYPE.get(extension.toLowerCase()); |
142 } else { |
156 } else { |
143 return null; |
157 return null; |
144 } |
158 } |
|
159 } |
|
160 |
|
161 public static File getPreviewerResource(String name) { |
|
162 try { |
|
163 IPath path = getResourcePath(name); |
|
164 if (path != null && path.segmentCount() > 1) { |
|
165 if (path.segmentCount() == 2 && HttpPreviewer.PREVIEW_STARTING_PAGE.equals(path.segment(1))) { |
|
166 path = new Path(PreviewerStaticResourceProvider.PREVIEW_START); |
|
167 } else if (path.segmentCount() > 2 |
|
168 && PreviewerStaticResourceProvider.PREVIEW_PATH.equals(path.segment(1))) { |
|
169 path = path.removeFirstSegments(1); |
|
170 } else { |
|
171 return null; |
|
172 } |
|
173 URL pluginResource = FileLocator.find(PreviewerPlugin.getDefault().getBundle(), path, null); |
|
174 if (pluginResource != null) { |
|
175 URL url = FileLocator.toFileURL(pluginResource); |
|
176 if (url != null) { |
|
177 return new File(url.getPath()); |
|
178 } |
|
179 } |
|
180 } |
|
181 } catch (IOException e) { |
|
182 PreviewerPlugin.log(e); |
|
183 } |
|
184 return null; |
145 } |
185 } |
146 |
186 |
147 public static URI getPreviewerStartingPage(String widget) { |
187 public static URI getPreviewerStartingPage(String widget) { |
148 return getServerURIForResource(new Path(widget).append(HttpPreviewer.PREVIEW_STARTING_PAGE).makeAbsolute(), |
188 return getServerURIForResource(new Path(widget).append(HttpPreviewer.PREVIEW_STARTING_PAGE).makeAbsolute(), |
149 null); |
189 null); |