Ejemplo n.º 1
0
  /**
   * Returns paths constructed by rewriting pathInfo using rules from the hosted site's mappings.
   * Paths are ordered from most to least specific match.
   *
   * @param site The hosted site.
   * @param pathInfo Path to be rewritten.
   * @param queryString
   * @return The rewritten path. May be either:
   *     <ul>
   *       <li>A path to a file in the Orion workspace, eg. <code>/ProjectA/foo/bar.txt</code>
   *       <li>An absolute URL pointing to another site, eg. <code>http://foo.com/bar.txt</code>
   *     </ul>
   *
   * @return The rewritten paths.
   * @throws URISyntaxException
   */
  private URI[] getMapped(IHostedSite site, IPath pathInfo, String queryString)
      throws URISyntaxException {
    final Map<String, List<String>> map = site.getMappings();
    final IPath originalPath = pathInfo;
    IPath path = originalPath.removeTrailingSeparator();

    List<URI> uris = new ArrayList<URI>();
    String rest = null;
    final int count = path.segmentCount();
    for (int i = 0; i <= count; i++) {
      List<String> base = map.get(path.toString());
      if (base != null) {
        rest = originalPath.removeFirstSegments(count - i).toString();
        for (int j = 0; j < base.size(); j++) {
          URI uri =
              rest.equals("") ? new URI(base.get(j)) : URIUtil.append(new URI(base.get(j)), rest);
          uris.add(
              new URI(
                  uri.getScheme(),
                  uri.getUserInfo(),
                  uri.getHost(),
                  uri.getPort(),
                  uri.getPath(),
                  queryString,
                  uri.getFragment()));
        }
      }
      path = path.removeLastSegments(1);
    }
    if (uris.size() == 0)
      // No mapping for /
      return null;
    else return uris.toArray(new URI[uris.size()]);
  }
Ejemplo n.º 2
0
  // returns true if the request has been served, false if not (only if failEarlyOn404 is true)
  private boolean serveOrionFile(
      HttpServletRequest req,
      HttpServletResponse resp,
      IHostedSite site,
      IPath path,
      boolean failEarlyOn404)
      throws ServletException {
    String userName = site.getUserName();
    String workspaceId = site.getWorkspaceId();
    String workspaceUri = WORKSPACE_SERVLET_ALIAS + "/" + workspaceId; // $NON-NLS-1$
    boolean allow = false;
    // Check that user who launched the hosted site really has access to the workspace
    try {
      if (AuthorizationService.checkRights(userName, workspaceUri, "GET")) { // $NON-NLS-1$
        allow = true;
      }
    } catch (JSONException e) {
      throw new ServletException(e);
    }

    if (allow) {
      // FIXME: this code is copied from NewFileServlet, fix it
      // start copied
      String pathInfo = path.toString();
      IPath filePath = pathInfo == null ? Path.ROOT : new Path(pathInfo);
      IFileStore file = tempGetFileStore(filePath);
      if (file == null || !file.fetchInfo().exists()) {
        if (failEarlyOn404) {
          return false;
        }
        handleException(
            resp,
            new ServerStatus(IStatus.ERROR, 404, NLS.bind("File not found: {0}", filePath), null));
      }
      if (fileSerializer.handleRequest(req, resp, file)) {
        // return;
      }
      // end copied

      if (file != null) {
        addEditHeaders(resp, site, path);
        addContentTypeHeader(resp, path);
      }
    } else {
      String msg = NLS.bind("No rights to access {0}", workspaceUri);
      handleException(
          resp, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_FORBIDDEN, msg, null));
    }
    return true;
  }
Ejemplo n.º 3
0
 private void addEditHeaders(HttpServletResponse resp, IHostedSite site, IPath path) {
   resp.addHeader(
       "X-Edit-Server", site.getEditServerUrl() + "/edit/edit.html#"); // $NON-NLS-1$ //$NON-NLS-2$
   resp.addHeader("X-Edit-Token", FILE_SERVLET_ALIAS + path.toString()); // $NON-NLS-1$
 }