예제 #1
0
  /**
   * Creates a new FTPFile instance by converting the given file: URI into an abstract pathname.
   *
   * <p>FTP URI protocol:<br>
   * ftp:// [ userName [ : password ] @ ] host [ : port ][ / path ]
   *
   * <p>example:<br>
   * ftp://[email protected]:21/pub/testfile.txt
   *
   * @param uri An absolute, hierarchical URI using a supported scheme.
   * @throws NullPointerException if <code>uri</code> is <code>null</code>.
   * @throws IllegalArgumentException If the preconditions on the parameter do not hold.
   */
  public FTPFile(URI uri) throws IOException, URISyntaxException {
    super(uri);

    if (uri.getScheme().equals("ftp")) {
      String userinfo = uri.getUserInfo();
      if (userinfo != null) {
        int index = userinfo.indexOf(":");
        if (index >= 0) {
          setFileSystem(
              new FTPFileSystem(
                  new FTPAccount(
                      uri.getHost(),
                      uri.getPort(),
                      userinfo.substring(0, index - 1),
                      userinfo.substring(index + 1),
                      uri.getPath())));
        } else {
          setFileSystem(
              new FTPFileSystem(
                  new FTPAccount(uri.getHost(), uri.getPort(), userinfo, "", uri.getPath())));
        }
      } else {
        fileSystem =
            new FTPFileSystem(
                new FTPAccount(uri.getHost(), uri.getPort(), null, "", uri.getPath()));
      }

      setFileName(uri.getPath());
      ftpClient = ((FTPFileSystem) fileSystem).getFTPClient();
    } else {
      throw new URISyntaxException(uri.toString(), "Wrong URI scheme");
    }
  }
예제 #2
0
  private void setPath(String path) throws URISyntaxException, IOException {
    // path starts with a /

    // get first component:
    int index = path.indexOf('/', 1);
    String first = path.substring(1, index);
    String rest = path.substring(index + 1);

    // the first part of the path is very similar to a url, due to the fact
    // it
    // may contain guest credentials. So I use the URI class to help me
    // parse it.
    // first as a url of its own. http = dymm
    URI url2 = new URI("dummy://" + first);
    String uinfo;
    credentials = new NamePasswordAuthentication();
    vmIdentifier = url2.getHost();

    String guestCred = fileURL.getProperty(GUEST_CREDENTIALS);
    if ((guestCred != null) && (!guestCred.isEmpty())) {
      uinfo = guestCred;
    } else {
      uinfo = url2.getUserInfo();
    }

    if (uinfo == null) {
      throw new IOException(
          "No guest credentials provided. please start the connection from the UI");
    }

    int indexOf = uinfo.indexOf(":");
    if (indexOf == -1) {
      throw new IOException(
          "No guest credentials provided. please start the connection from the UI");
    }

    guestUser = uinfo.substring(0, indexOf);
    guestPass = uinfo.substring(indexOf + 1);
    credentials.setInteractiveSession(false);
    credentials.setUsername(guestUser);
    credentials.setPassword(guestPass);

    pathInsideVm = rest;
  }
예제 #3
0
 private boolean identifyNewCommitResource(
     HttpServletRequest request, HttpServletResponse response, Repository db, String newCommit)
     throws ServletException {
   try {
     URI u = getURI(request);
     IPath p = new Path(u.getPath());
     IPath np = new Path("/"); // $NON-NLS-1$
     for (int i = 0; i < p.segmentCount(); i++) {
       String s = p.segment(i);
       if (i == 2) {
         s += ".." + newCommit; // $NON-NLS-1$
       }
       np = np.append(s);
     }
     if (p.hasTrailingSeparator()) np = np.addTrailingSeparator();
     URI nu =
         new URI(
             u.getScheme(),
             u.getUserInfo(),
             u.getHost(),
             u.getPort(),
             np.toString(),
             u.getQuery(),
             u.getFragment());
     JSONObject result = new JSONObject();
     result.put(ProtocolConstants.KEY_LOCATION, nu);
     OrionServlet.writeJSONResponse(
         request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT);
     response.setHeader(
         ProtocolConstants.HEADER_LOCATION, resovleOrionURI(request, nu).toString());
     return true;
   } catch (Exception e) {
     return statusHandler.handleRequest(
         request,
         response,
         new ServerStatus(
             IStatus.ERROR,
             HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
             "An error occured when identifying a new Commit resource.",
             e));
   }
 }
  /**
   * Decodes a Pop3Store URI.
   *
   * <p>Possible forms:
   *
   * <pre>
   * pop3://authType:user:password@server:port
   *      ConnectionSecurity.NONE
   * pop3+tls+://authType:user:password@server:port
   *      ConnectionSecurity.STARTTLS_REQUIRED
   * pop3+ssl+://authType:user:password@server:port
   *      ConnectionSecurity.SSL_TLS_REQUIRED
   * </pre>
   *
   * e.g.
   *
   * <pre>pop3://PLAIN:admin:[email protected]:12345</pre>
   */
  public static ServerSettings decodeUri(String uri) {
    String host;
    int port;
    ConnectionSecurity connectionSecurity;
    String username = null;
    String password = null;
    String clientCertificateAlias = null;

    URI pop3Uri;
    try {
      pop3Uri = new URI(uri);
    } catch (URISyntaxException use) {
      throw new IllegalArgumentException("Invalid Pop3Store URI", use);
    }

    String scheme = pop3Uri.getScheme();
    /*
     * Currently available schemes are:
     * pop3
     * pop3+tls+
     * pop3+ssl+
     *
     * The following are obsolete schemes that may be found in pre-existing
     * settings from earlier versions or that may be found when imported. We
     * continue to recognize them and re-map them appropriately:
     * pop3+tls
     * pop3+ssl
     */
    if (scheme.equals("pop3")) {
      connectionSecurity = ConnectionSecurity.NONE;
      port = Type.POP3.defaultPort;
    } else if (scheme.startsWith("pop3+tls")) {
      connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;
      port = Type.POP3.defaultPort;
    } else if (scheme.startsWith("pop3+ssl")) {
      connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
      port = Type.POP3.defaultTlsPort;
    } else {
      throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
    }

    host = pop3Uri.getHost();

    if (pop3Uri.getPort() != -1) {
      port = pop3Uri.getPort();
    }

    AuthType authType = AuthType.PLAIN;
    if (pop3Uri.getUserInfo() != null) {
      int userIndex = 0, passwordIndex = 1;
      String userinfo = pop3Uri.getUserInfo();
      String[] userInfoParts = userinfo.split(":");
      if (userInfoParts.length > 2 || userinfo.endsWith(":")) {
        // If 'userinfo' ends with ":" the password is empty. This can only happen
        // after an account was imported (so authType and username are present).
        userIndex++;
        passwordIndex++;
        authType = AuthType.valueOf(userInfoParts[0]);
      }
      username = decodeUtf8(userInfoParts[userIndex]);
      if (userInfoParts.length > passwordIndex) {
        if (authType == AuthType.EXTERNAL) {
          clientCertificateAlias = decodeUtf8(userInfoParts[passwordIndex]);
        } else {
          password = decodeUtf8(userInfoParts[passwordIndex]);
        }
      }
    }

    return new ServerSettings(
        ServerSettings.Type.POP3,
        host,
        port,
        connectionSecurity,
        authType,
        username,
        password,
        clientCertificateAlias);
  }