plugins/org.symbian.tools.tmw.previewer/src/org/symbian/tools/tmw/previewer/http/WorkspaceResourcesServlet.java
changeset 479 518afa7c6d2f
parent 472 bd9f2d7c64a6
child 483 109da596fa9d
equal deleted inserted replaced
478:6c07c755d0c7 479:518afa7c6d2f
    48 import org.eclipse.core.runtime.IPath;
    48 import org.eclipse.core.runtime.IPath;
    49 import org.eclipse.core.runtime.Path;
    49 import org.eclipse.core.runtime.Path;
    50 import org.json.simple.JSONObject;
    50 import org.json.simple.JSONObject;
    51 import org.json.simple.parser.JSONParser;
    51 import org.json.simple.parser.JSONParser;
    52 import org.json.simple.parser.ParseException;
    52 import org.json.simple.parser.ParseException;
       
    53 import org.symbian.tools.tmw.core.TMWCore;
       
    54 import org.symbian.tools.tmw.core.projects.ITMWProject;
       
    55 import org.symbian.tools.tmw.core.runtimes.IApplicationLayoutProvider;
    53 import org.symbian.tools.tmw.previewer.PreviewerException;
    56 import org.symbian.tools.tmw.previewer.PreviewerException;
    54 import org.symbian.tools.tmw.previewer.PreviewerPlugin;
    57 import org.symbian.tools.tmw.previewer.PreviewerPlugin;
    55 import org.symbian.tools.tmw.previewer.core.IApplicationLayoutProvider;
       
    56 import org.symbian.tools.tmw.previewer.http.handlers.DebuggerResourceProvider;
    58 import org.symbian.tools.tmw.previewer.http.handlers.DebuggerResourceProvider;
    57 import org.symbian.tools.tmw.previewer.http.handlers.PreviewerStaticResourceProvider;
    59 import org.symbian.tools.tmw.previewer.http.handlers.PreviewerStaticResourceProvider;
    58 import org.symbian.tools.tmw.previewer.http.handlers.Providers;
    60 import org.symbian.tools.tmw.previewer.http.handlers.Providers;
    59 
    61 
    60 public class WorkspaceResourcesServlet extends HttpServlet {
    62 public class WorkspaceResourcesServlet extends HttpServlet {
    61     private static final Map<String, String> EXTENSION_CONTENT_TYPE = new TreeMap<String, String>();
    63     private static final Map<String, String> EXTENSION_CONTENT_TYPE = new TreeMap<String, String>();
    62 
       
    63     private static final long serialVersionUID = -3217197074249607950L;
    64     private static final long serialVersionUID = -3217197074249607950L;
    64 
    65 
    65     static {
    66     static {
    66         EXTENSION_CONTENT_TYPE.put("htm", "text/html");
    67         EXTENSION_CONTENT_TYPE.put("htm", "text/html");
    67         EXTENSION_CONTENT_TYPE.put("html", "text/html");
    68         EXTENSION_CONTENT_TYPE.put("html", "text/html");
    97             // Something is horribly wrong - JRE doesn't have UTF8?
    98             // Something is horribly wrong - JRE doesn't have UTF8?
    98             throw new RuntimeException(e);
    99             throw new RuntimeException(e);
    99         }
   100         }
   100     }
   101     }
   101 
   102 
       
   103     public static URI getDebugStartingPage(IProject project, String session) {
       
   104         return getServerURIForResource(project.getFullPath().append(HttpPreviewer.DEBUG_STARTING_PAGE).makeAbsolute(),
       
   105                 session);
       
   106     }
       
   107 
   102     public static IFile getFileFromUrl(String name) {
   108     public static IFile getFileFromUrl(String name) {
   103         final IPath path = getResourcePath(name);
   109         final IPath path = getResourcePath(name);
   104         if (path != null) {
   110         if (path != null) {
   105             final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.segment(0));
   111             final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.segment(0));
   106             final IApplicationLayoutProvider provider = PreviewerPlugin.getExtensionsManager().getLayoutProvider(
   112             final ITMWProject p = TMWCore.create(project);
   107                     project);
   113             if (p != null && p.getTargetRuntime() != null) {
   108             if (provider != null) {
   114                 final IApplicationLayoutProvider provider = p.getTargetRuntime().getLayoutProvider();
   109                 try {
   115                 if (provider != null) {
   110                     return provider.getWorkspaceFile(project, path.removeFirstSegments(1));
   116                     try {
   111                 } catch (CoreException e) {
   117                         return provider.getWorkspaceFile(project, path.removeFirstSegments(1));
   112                     PreviewerPlugin.log(e);
   118                     } catch (CoreException e) {
       
   119                         PreviewerPlugin.log(e);
       
   120                     }
   113                 }
   121                 }
   114             }
   122             }
   115         }
   123         }
   116         return null;
   124         return null;
   117     }
       
   118 
       
   119     private static IPath getResourcePath(String name) {
       
   120         IPath p = null;
       
   121         try {
       
   122             String root = getHttpUrl(null);
       
   123             if (name != null && name.startsWith(root)) {
       
   124                 final String fileName = URLDecoder.decode(name.substring(root.length()), "UTF-8");
       
   125                 final IPath path = new Path(fileName);
       
   126                 if (path.segmentCount() > 1) {
       
   127                     p = path;
       
   128                 }
       
   129             }
       
   130         } catch (UnsupportedEncodingException e) {
       
   131             throw new RuntimeException(e);
       
   132         }
       
   133         return p;
       
   134     }
   125     }
   135 
   126 
   136     public static String getHttpUrl(IFile file) {
   127     public static String getHttpUrl(IFile file) {
   137         IPath p = new Path("/");
   128         IPath p = new Path("/");
   138         if (file != null) {
   129         if (file != null) {
   139             IApplicationLayoutProvider provider = PreviewerPlugin.getExtensionsManager().getLayoutProvider(
   130             final ITMWProject project = TMWCore.create(file.getProject());
   140                     file.getProject());
   131             if (project != null && project.getTargetRuntime() != null) {
   141             if (provider != null) {
   132                 final IApplicationLayoutProvider provider = project.getTargetRuntime().getLayoutProvider();
   142                 IPath path = provider.getResourcePath(file);
   133                 if (provider != null) {
   143                 if (path != null) {
   134                     IPath path = provider.getResourcePath(file);
   144                     p = p.append(file.getProject().getName()).append(path).makeAbsolute();
   135                     if (path != null) {
       
   136                         p = p.append(file.getProject().getName()).append(path).makeAbsolute();
       
   137                     }
   145                 }
   138                 }
   146             }
   139             }
   147         }
   140         }
   148         String uri = getServerURIForResource(p, null).toASCIIString();
   141         String uri = getServerURIForResource(p, null).toASCIIString();
   149         if (uri != null) {
   142         if (uri != null) {
   190     public static URI getPreviewerStartingPage(String widget) {
   183     public static URI getPreviewerStartingPage(String widget) {
   191         return getServerURIForResource(new Path(widget).append(HttpPreviewer.PREVIEW_STARTING_PAGE).makeAbsolute(),
   184         return getServerURIForResource(new Path(widget).append(HttpPreviewer.PREVIEW_STARTING_PAGE).makeAbsolute(),
   192                 null);
   185                 null);
   193     }
   186     }
   194 
   187 
       
   188     private static IPath getResourcePath(String name) {
       
   189         IPath p = null;
       
   190         try {
       
   191             String root = getHttpUrl(null);
       
   192             if (name != null && name.startsWith(root)) {
       
   193                 final String fileName = URLDecoder.decode(name.substring(root.length()), "UTF-8");
       
   194                 final IPath path = new Path(fileName);
       
   195                 if (path.segmentCount() > 1) {
       
   196                     p = path;
       
   197                 }
       
   198             }
       
   199         } catch (UnsupportedEncodingException e) {
       
   200             throw new RuntimeException(e);
       
   201         }
       
   202         return p;
       
   203     }
       
   204 
   195     private static URI getServerURIForResource(IPath resourcePath, String debugSessionId) {
   205     private static URI getServerURIForResource(IPath resourcePath, String debugSessionId) {
   196         try {
   206         try {
   197             String path = encode(WebappManager.WORKSPACE_RESOURCES_CONTEXT + resourcePath);
   207             String path = encode(WebappManager.WORKSPACE_RESOURCES_CONTEXT + resourcePath);
   198             path += debugSessionId == null ? "" : (String.format("?%s=%s",
   208             path += debugSessionId == null ? "" : (String.format("?%s=%s",
   199                     DebuggerResourceProvider.DEBUG_SESSION_ID_PARAMETER, debugSessionId));
   209                     DebuggerResourceProvider.DEBUG_SESSION_ID_PARAMETER, debugSessionId));
   271         } catch (PreviewerException e) {
   281         } catch (PreviewerException e) {
   272             throw new ServletException(e);
   282             throw new ServletException(e);
   273         }
   283         }
   274         resp.setStatus(HttpServletResponse.SC_OK);
   284         resp.setStatus(HttpServletResponse.SC_OK);
   275     }
   285     }
   276 
       
   277     public static URI getDebugStartingPage(IProject project, String session) {
       
   278         return getServerURIForResource(project.getFullPath().append(HttpPreviewer.DEBUG_STARTING_PAGE).makeAbsolute(),
       
   279                 session);
       
   280     }
       
   281 }
   286 }