Example usage for java.net URI getUserInfo

List of usage examples for java.net URI getUserInfo

Introduction

In this page you can find the example usage for java.net URI getUserInfo.

Prototype

public String getUserInfo() 

Source Link

Document

Returns the decoded user-information component of this URI.

Usage

From source file:com.fsck.k9.mail.store.webdav.WebDavStore.java

/**
 * Performs form-based authentication./*  w ww . j  ava  2 s . c  o m*/
 *
 * @throws MessagingException
 */
public void doFBA(ConnectionInfo info) throws IOException, MessagingException {
    // Clear out cookies from any previous authentication.
    mAuthCookies.clear();

    WebDavHttpClient httpClient = getHttpClient();

    String loginUrl;
    if (info != null) {
        loginUrl = info.guessedAuthUrl;
    } else if (mCachedLoginUrl != null && !mCachedLoginUrl.equals("")) {
        loginUrl = mCachedLoginUrl;
    } else {
        throw new MessagingException("No valid login URL available for form-based authentication.");
    }

    HttpGeneric request = new HttpGeneric(loginUrl);
    request.setMethod("POST");

    // Build the POST data.
    List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
    pairs.add(new BasicNameValuePair("destination", mUrl));
    pairs.add(new BasicNameValuePair("username", mUsername));
    pairs.add(new BasicNameValuePair("password", mPassword));
    pairs.add(new BasicNameValuePair("flags", "0"));
    pairs.add(new BasicNameValuePair("SubmitCreds", "Log+On"));
    pairs.add(new BasicNameValuePair("forcedownlevel", "0"));
    pairs.add(new BasicNameValuePair("trusted", "0"));

    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs);
    request.setEntity(formEntity);

    HttpResponse response = httpClient.executeOverride(request, mContext);
    boolean authenticated = testAuthenticationResponse(response);
    if (!authenticated) {
        // Check the response from the authentication request above for a form action.
        String formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity()));
        if (formAction == null) {
            // If there is no form action, try using our redirect URL from the initial connection.
            if (info != null && info.redirectUrl != null && !info.redirectUrl.equals("")) {
                loginUrl = info.redirectUrl;

                request = new HttpGeneric(loginUrl);
                request.setMethod("GET");

                response = httpClient.executeOverride(request, mContext);
                formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity()));
            }
        }
        if (formAction != null) {
            try {
                URI formActionUri = new URI(formAction);
                URI loginUri = new URI(loginUrl);

                if (formActionUri.isAbsolute()) {
                    // The form action is an absolute URL, just use it.
                    loginUrl = formAction;
                } else {
                    // Append the form action to our current URL, minus the file name.
                    String urlPath;
                    if (formAction.startsWith("/")) {
                        urlPath = formAction;
                    } else {
                        urlPath = loginUri.getPath();
                        int lastPathPos = urlPath.lastIndexOf('/');
                        if (lastPathPos > -1) {
                            urlPath = urlPath.substring(0, lastPathPos + 1);
                            urlPath = urlPath.concat(formAction);
                        }
                    }

                    // Reconstruct the login URL based on the original login URL and the form action.
                    URI finalUri = new URI(loginUri.getScheme(), loginUri.getUserInfo(), loginUri.getHost(),
                            loginUri.getPort(), urlPath, null, null);
                    loginUrl = finalUri.toString();
                }

                // Retry the login using our new URL.
                request = new HttpGeneric(loginUrl);
                request.setMethod("POST");
                request.setEntity(formEntity);

                response = httpClient.executeOverride(request, mContext);
                authenticated = testAuthenticationResponse(response);
            } catch (URISyntaxException e) {
                Log.e(LOG_TAG, "URISyntaxException caught " + e + "\nTrace: " + processException(e));
                throw new MessagingException("URISyntaxException caught", e);
            }
        } else {
            throw new MessagingException("A valid URL for Exchange authentication could not be found.");
        }
    }

    if (authenticated) {
        mAuthentication = AUTH_TYPE_FORM_BASED;
        mCachedLoginUrl = loginUrl;
    } else {
        throw new MessagingException("Invalid credentials provided for authentication.");
    }
}

From source file:br.pcfl.up.mail.store.WebDavStore.java

/**
 * Performs form-based authentication.//from  www.  j  a va  2 s .c o  m
 *
 * @throws MessagingException
 */
public void doFBA(ConnectionInfo info) throws IOException, MessagingException {
    // Clear out cookies from any previous authentication.
    mAuthCookies.clear();

    WebDavHttpClient httpClient = getHttpClient();

    String loginUrl;
    if (info != null) {
        loginUrl = info.guessedAuthUrl;
    } else if (mCachedLoginUrl != null && !mCachedLoginUrl.equals("")) {
        loginUrl = mCachedLoginUrl;
    } else {
        throw new MessagingException("No valid login URL available for form-based authentication.");
    }

    HttpGeneric request = new HttpGeneric(loginUrl);
    request.setMethod("POST");

    // Build the POST data.
    ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
    pairs.add(new BasicNameValuePair("destination", mUrl));
    pairs.add(new BasicNameValuePair("username", mUsername));
    pairs.add(new BasicNameValuePair("password", mPassword));
    pairs.add(new BasicNameValuePair("flags", "0"));
    pairs.add(new BasicNameValuePair("SubmitCreds", "Log+On"));
    pairs.add(new BasicNameValuePair("forcedownlevel", "0"));
    pairs.add(new BasicNameValuePair("trusted", "0"));

    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs);
    request.setEntity(formEntity);

    HttpResponse response = httpClient.executeOverride(request, mContext);
    boolean authenticated = testAuthenticationResponse(response);
    if (!authenticated) {
        // Check the response from the authentication request above for a form action.
        String formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity()));
        if (formAction == null) {
            // If there is no form action, try using our redirect URL from the initial connection.
            if (info != null && info.redirectUrl != null && !info.redirectUrl.equals("")) {
                loginUrl = info.redirectUrl;

                request = new HttpGeneric(loginUrl);
                request.setMethod("GET");

                response = httpClient.executeOverride(request, mContext);
                formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity()));
            }
        }
        if (formAction != null) {
            try {
                URI formActionUri = new URI(formAction);
                URI loginUri = new URI(loginUrl);

                if (formActionUri.isAbsolute()) {
                    // The form action is an absolute URL, just use it.
                    loginUrl = formAction;
                } else {
                    // Append the form action to our current URL, minus the file name.
                    String urlPath;
                    if (formAction.startsWith("/")) {
                        urlPath = formAction;
                    } else {
                        urlPath = loginUri.getPath();
                        int lastPathPos = urlPath.lastIndexOf('/');
                        if (lastPathPos > -1) {
                            urlPath = urlPath.substring(0, lastPathPos + 1);
                            urlPath = urlPath.concat(formAction);
                        }
                    }

                    // Reconstruct the login URL based on the original login URL and the form action.
                    URI finalUri = new URI(loginUri.getScheme(), loginUri.getUserInfo(), loginUri.getHost(),
                            loginUri.getPort(), urlPath, null, null);
                    loginUrl = finalUri.toString();
                }

                // Retry the login using our new URL.
                request = new HttpGeneric(loginUrl);
                request.setMethod("POST");
                request.setEntity(formEntity);

                response = httpClient.executeOverride(request, mContext);
                authenticated = testAuthenticationResponse(response);
            } catch (URISyntaxException e) {
                Log.e(Up.LOG_TAG, "URISyntaxException caught " + e + "\nTrace: " + processException(e));
                throw new MessagingException("URISyntaxException caught", e);
            }
        } else {
            throw new MessagingException("A valid URL for Exchange authentication could not be found.");
        }
    }

    if (authenticated) {
        mAuthentication = AUTH_TYPE_FORM_BASED;
        mCachedLoginUrl = loginUrl;
    } else {
        throw new MessagingException("Invalid credentials provided for authentication.");
    }
}

From source file:cn.mailchat.mail.store.WebDavStore.java

/**
 * Performs form-based authentication./*from www .j  av a2s.c  o m*/
 *
 * @throws MessagingException
 */
public void doFBA(ConnectionInfo info) throws IOException, MessagingException {
    // Clear out cookies from any previous authentication.
    mAuthCookies.clear();

    WebDavHttpClient httpClient = getHttpClient();

    String loginUrl;
    if (info != null) {
        loginUrl = info.guessedAuthUrl;
    } else if (mCachedLoginUrl != null && !mCachedLoginUrl.equals("")) {
        loginUrl = mCachedLoginUrl;
    } else {
        throw new MessagingException("No valid login URL available for form-based authentication.");
    }

    HttpGeneric request = new HttpGeneric(loginUrl);
    request.setMethod("POST");

    // Build the POST data.
    ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
    pairs.add(new BasicNameValuePair("destination", mUrl));
    pairs.add(new BasicNameValuePair("username", mUsername));
    pairs.add(new BasicNameValuePair("password", mPassword));
    pairs.add(new BasicNameValuePair("flags", "0"));
    pairs.add(new BasicNameValuePair("SubmitCreds", "Log+On"));
    pairs.add(new BasicNameValuePair("forcedownlevel", "0"));
    pairs.add(new BasicNameValuePair("trusted", "0"));

    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs);
    request.setEntity(formEntity);

    HttpResponse response = httpClient.executeOverride(request, mContext);
    boolean authenticated = testAuthenticationResponse(response);
    if (!authenticated) {
        // Check the response from the authentication request above for a form action.
        String formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity()));
        if (formAction == null) {
            // If there is no form action, try using our redirect URL from the initial connection.
            if (info != null && info.redirectUrl != null && !info.redirectUrl.equals("")) {
                loginUrl = info.redirectUrl;

                request = new HttpGeneric(loginUrl);
                request.setMethod("GET");

                response = httpClient.executeOverride(request, mContext);
                formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity()));
            }
        }
        if (formAction != null) {
            try {
                URI formActionUri = new URI(formAction);
                URI loginUri = new URI(loginUrl);

                if (formActionUri.isAbsolute()) {
                    // The form action is an absolute URL, just use it.
                    loginUrl = formAction;
                } else {
                    // Append the form action to our current URL, minus the file name.
                    String urlPath;
                    if (formAction.startsWith("/")) {
                        urlPath = formAction;
                    } else {
                        urlPath = loginUri.getPath();
                        int lastPathPos = urlPath.lastIndexOf('/');
                        if (lastPathPos > -1) {
                            urlPath = urlPath.substring(0, lastPathPos + 1);
                            urlPath = urlPath.concat(formAction);
                        }
                    }

                    // Reconstruct the login URL based on the original login URL and the form action.
                    URI finalUri = new URI(loginUri.getScheme(), loginUri.getUserInfo(), loginUri.getHost(),
                            loginUri.getPort(), urlPath, null, null);
                    loginUrl = finalUri.toString();
                }

                // Retry the login using our new URL.
                request = new HttpGeneric(loginUrl);
                request.setMethod("POST");
                request.setEntity(formEntity);

                response = httpClient.executeOverride(request, mContext);
                authenticated = testAuthenticationResponse(response);
            } catch (URISyntaxException e) {
                Log.e(MailChat.LOG_TAG, "URISyntaxException caught " + e + "\nTrace: " + processException(e));
                throw new MessagingException("URISyntaxException caught", e);
            }
        } else {
            throw new MessagingException("A valid URL for Exchange authentication could not be found.");
        }
    }

    if (authenticated) {
        mAuthentication = AUTH_TYPE_FORM_BASED;
        mCachedLoginUrl = loginUrl;
    } else {
        throw new MessagingException("Invalid credentials provided for authentication.");
    }
}

From source file:com.top.Ertebat.mail.store.WebDavStore.java

/**
 * Performs form-based authentication.//from  w w w. j av a 2s .co m
 *
 * @throws MessagingException
 */
public void doFBA(ConnectionInfo info) throws IOException, MessagingException {
    // Clear out cookies from any previous authentication.
    mAuthCookies.clear();

    WebDavHttpClient httpClient = getHttpClient();

    String loginUrl;
    if (info != null) {
        loginUrl = info.guessedAuthUrl;
    } else if (mCachedLoginUrl != null && !mCachedLoginUrl.equals("")) {
        loginUrl = mCachedLoginUrl;
    } else {
        throw new MessagingException("No valid login URL available for form-based authentication.");
    }

    HttpGeneric request = new HttpGeneric(loginUrl);
    request.setMethod("POST");

    // Build the POST data.
    ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
    pairs.add(new BasicNameValuePair("destination", mUrl));
    pairs.add(new BasicNameValuePair("username", mUsername));
    pairs.add(new BasicNameValuePair("password", mPassword));
    pairs.add(new BasicNameValuePair("flags", "0"));
    pairs.add(new BasicNameValuePair("SubmitCreds", "Log+On"));
    pairs.add(new BasicNameValuePair("forcedownlevel", "0"));
    pairs.add(new BasicNameValuePair("trusted", "0"));

    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs);
    request.setEntity(formEntity);

    HttpResponse response = httpClient.executeOverride(request, mContext);
    boolean authenticated = testAuthenticationResponse(response);
    if (!authenticated) {
        // Check the response from the authentication request above for a form action.
        String formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity()));
        if (formAction == null) {
            // If there is no form action, try using our redirect URL from the initial connection.
            if (info != null && info.redirectUrl != null && !info.redirectUrl.equals("")) {
                loginUrl = info.redirectUrl;

                request = new HttpGeneric(loginUrl);
                request.setMethod("GET");

                response = httpClient.executeOverride(request, mContext);
                formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity()));
            }
        }
        if (formAction != null) {
            try {
                URI formActionUri = new URI(formAction);
                URI loginUri = new URI(loginUrl);

                if (formActionUri.isAbsolute()) {
                    // The form action is an absolute URL, just use it.
                    loginUrl = formAction;
                } else {
                    // Append the form action to our current URL, minus the file name.
                    String urlPath;
                    if (formAction.startsWith("/")) {
                        urlPath = formAction;
                    } else {
                        urlPath = loginUri.getPath();
                        int lastPathPos = urlPath.lastIndexOf('/');
                        if (lastPathPos > -1) {
                            urlPath = urlPath.substring(0, lastPathPos + 1);
                            urlPath = urlPath.concat(formAction);
                        }
                    }

                    // Reconstruct the login URL based on the original login URL and the form action.
                    URI finalUri = new URI(loginUri.getScheme(), loginUri.getUserInfo(), loginUri.getHost(),
                            loginUri.getPort(), urlPath, null, null);
                    loginUrl = finalUri.toString();
                }

                // Retry the login using our new URL.
                request = new HttpGeneric(loginUrl);
                request.setMethod("POST");
                request.setEntity(formEntity);

                response = httpClient.executeOverride(request, mContext);
                authenticated = testAuthenticationResponse(response);
            } catch (URISyntaxException e) {
                Log.e(Ertebat.LOG_TAG, "URISyntaxException caught " + e + "\nTrace: " + processException(e));
                throw new MessagingException("URISyntaxException caught", e);
            }
        } else {
            throw new MessagingException("A valid URL for Exchange authentication could not be found.");
        }
    }

    if (authenticated) {
        mAuthentication = AUTH_TYPE_FORM_BASED;
        mCachedLoginUrl = loginUrl;
    } else {
        throw new MessagingException("Invalid credentials provided for authentication.");
    }
}

From source file:com.c0124.k9.mail.store.WebDavStore.java

/**
 * Performs form-based authentication.//w w  w  . j  ava 2 s  .  c o  m
 *
 * @throws MessagingException
 */
public void doFBA(ConnectionInfo info) throws IOException, MessagingException {
    // Clear out cookies from any previous authentication.
    mAuthCookies.clear();

    WebDavHttpClient httpClient = getHttpClient();

    String loginUrl;
    if (info != null) {
        loginUrl = info.guessedAuthUrl;
    } else if (mCachedLoginUrl != null && !mCachedLoginUrl.equals("")) {
        loginUrl = mCachedLoginUrl;
    } else {
        throw new MessagingException("No valid login URL available for form-based authentication.");
    }

    HttpGeneric request = new HttpGeneric(loginUrl);
    request.setMethod("POST");

    // Build the POST data.
    ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
    pairs.add(new BasicNameValuePair("destination", mUrl));
    pairs.add(new BasicNameValuePair("username", mUsername));
    pairs.add(new BasicNameValuePair("password", mPassword));
    pairs.add(new BasicNameValuePair("flags", "0"));
    pairs.add(new BasicNameValuePair("SubmitCreds", "Log+On"));
    pairs.add(new BasicNameValuePair("forcedownlevel", "0"));
    pairs.add(new BasicNameValuePair("trusted", "0"));

    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs);
    request.setEntity(formEntity);

    HttpResponse response = httpClient.executeOverride(request, mContext);
    boolean authenticated = testAuthenticationResponse(response);
    if (!authenticated) {
        // Check the response from the authentication request above for a form action.
        String formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity()));
        if (formAction == null) {
            // If there is no form action, try using our redirect URL from the initial connection.
            if (info != null && info.redirectUrl != null && !info.redirectUrl.equals("")) {
                loginUrl = info.redirectUrl;

                request = new HttpGeneric(loginUrl);
                request.setMethod("GET");

                response = httpClient.executeOverride(request, mContext);
                formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity()));
            }
        }
        if (formAction != null) {
            try {
                URI formActionUri = new URI(formAction);
                URI loginUri = new URI(loginUrl);

                if (formActionUri.isAbsolute()) {
                    // The form action is an absolute URL, just use it.
                    loginUrl = formAction;
                } else {
                    // Append the form action to our current URL, minus the file name.
                    String urlPath;
                    if (formAction.startsWith("/")) {
                        urlPath = formAction;
                    } else {
                        urlPath = loginUri.getPath();
                        int lastPathPos = urlPath.lastIndexOf('/');
                        if (lastPathPos > -1) {
                            urlPath = urlPath.substring(0, lastPathPos + 1);
                            urlPath = urlPath.concat(formAction);
                        }
                    }

                    // Reconstruct the login URL based on the original login URL and the form action.
                    URI finalUri = new URI(loginUri.getScheme(), loginUri.getUserInfo(), loginUri.getHost(),
                            loginUri.getPort(), urlPath, null, null);
                    loginUrl = finalUri.toString();
                }

                // Retry the login using our new URL.
                request = new HttpGeneric(loginUrl);
                request.setMethod("POST");
                request.setEntity(formEntity);

                response = httpClient.executeOverride(request, mContext);
                authenticated = testAuthenticationResponse(response);
            } catch (URISyntaxException e) {
                Log.e(K9.LOG_TAG, "URISyntaxException caught " + e + "\nTrace: " + processException(e));
                throw new MessagingException("URISyntaxException caught", e);
            }
        } else {
            throw new MessagingException("A valid URL for Exchange authentication could not be found.");
        }
    }

    if (authenticated) {
        mAuthentication = AUTH_TYPE_FORM_BASED;
        mCachedLoginUrl = loginUrl;
    } else {
        throw new MessagingException("Invalid credentials provided for authentication.");
    }
}

From source file:com.bernard.beaconportal.activities.mail.store.WebDavStore.java

/**
 * Performs form-based authentication.//from  w  w  w  . j a v  a 2s  .com
 * 
 * @throws MessagingException
 */
public void doFBA(ConnectionInfo info) throws IOException, MessagingException {
    // Clear out cookies from any previous authentication.
    mAuthCookies.clear();

    WebDavHttpClient httpClient = getHttpClient();

    String loginUrl;
    if (info != null) {
        loginUrl = info.guessedAuthUrl;
    } else if (mCachedLoginUrl != null && !mCachedLoginUrl.equals("")) {
        loginUrl = mCachedLoginUrl;
    } else {
        throw new MessagingException("No valid login URL available for form-based authentication.");
    }

    HttpGeneric request = new HttpGeneric(loginUrl);
    request.setMethod("POST");

    // Build the POST data.
    ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
    pairs.add(new BasicNameValuePair("destination", mUrl));
    pairs.add(new BasicNameValuePair("username", mUsername));
    pairs.add(new BasicNameValuePair("password", mPassword));
    pairs.add(new BasicNameValuePair("flags", "0"));
    pairs.add(new BasicNameValuePair("SubmitCreds", "Log+On"));
    pairs.add(new BasicNameValuePair("forcedownlevel", "0"));
    pairs.add(new BasicNameValuePair("trusted", "0"));

    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs);
    request.setEntity(formEntity);

    HttpResponse response = httpClient.executeOverride(request, mContext);
    boolean authenticated = testAuthenticationResponse(response);
    if (!authenticated) {
        // Check the response from the authentication request above for a
        // form action.
        String formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity()));
        if (formAction == null) {
            // If there is no form action, try using our redirect URL from
            // the initial connection.
            if (info != null && info.redirectUrl != null && !info.redirectUrl.equals("")) {
                loginUrl = info.redirectUrl;

                request = new HttpGeneric(loginUrl);
                request.setMethod("GET");

                response = httpClient.executeOverride(request, mContext);
                formAction = findFormAction(WebDavHttpClient.getUngzippedContent(response.getEntity()));
            }
        }
        if (formAction != null) {
            try {
                URI formActionUri = new URI(formAction);
                URI loginUri = new URI(loginUrl);

                if (formActionUri.isAbsolute()) {
                    // The form action is an absolute URL, just use it.
                    loginUrl = formAction;
                } else {
                    // Append the form action to our current URL, minus the
                    // file name.
                    String urlPath;
                    if (formAction.startsWith("/")) {
                        urlPath = formAction;
                    } else {
                        urlPath = loginUri.getPath();
                        int lastPathPos = urlPath.lastIndexOf('/');
                        if (lastPathPos > -1) {
                            urlPath = urlPath.substring(0, lastPathPos + 1);
                            urlPath = urlPath.concat(formAction);
                        }
                    }

                    // Reconstruct the login URL based on the original login
                    // URL and the form action.
                    URI finalUri = new URI(loginUri.getScheme(), loginUri.getUserInfo(), loginUri.getHost(),
                            loginUri.getPort(), urlPath, null, null);
                    loginUrl = finalUri.toString();
                }

                // Retry the login using our new URL.
                request = new HttpGeneric(loginUrl);
                request.setMethod("POST");
                request.setEntity(formEntity);

                response = httpClient.executeOverride(request, mContext);
                authenticated = testAuthenticationResponse(response);
            } catch (URISyntaxException e) {
                Log.e(K9.LOG_TAG, "URISyntaxException caught " + e + "\nTrace: " + processException(e));
                throw new MessagingException("URISyntaxException caught", e);
            }
        } else {
            throw new MessagingException("A valid URL for Exchange authentication could not be found.");
        }
    }

    if (authenticated) {
        mAuthentication = AUTH_TYPE_FORM_BASED;
        mCachedLoginUrl = loginUrl;
    } else {
        throw new MessagingException("Invalid credentials provided for authentication.");
    }
}

From source file:org.apache.hadoop.hive.metastore.HiveMetaStoreClientPreCatalog.java

private void resolveUris() throws MetaException {
    String metastoreUrisString[] = MetastoreConf.getVar(conf, ConfVars.THRIFT_URIS).split(",");

    List<URI> metastoreURIArray = new ArrayList<URI>();
    try {//  www .j  a v  a2s. c o m
        int i = 0;
        for (String s : metastoreUrisString) {
            URI tmpUri = new URI(s);
            if (tmpUri.getScheme() == null) {
                throw new IllegalArgumentException("URI: " + s + " does not have a scheme");
            }
            if (uriResolverHook != null) {
                metastoreURIArray.addAll(uriResolverHook.resolveURI(tmpUri));
            } else {
                metastoreURIArray.add(new URI(tmpUri.getScheme(), tmpUri.getUserInfo(),
                        HadoopThriftAuthBridge.getBridge().getCanonicalHostName(tmpUri.getHost()),
                        tmpUri.getPort(), tmpUri.getPath(), tmpUri.getQuery(), tmpUri.getFragment()));
            }
        }
        metastoreUris = new URI[metastoreURIArray.size()];
        for (int j = 0; j < metastoreURIArray.size(); j++) {
            metastoreUris[j] = metastoreURIArray.get(j);
        }

        if (MetastoreConf.getVar(conf, ConfVars.THRIFT_URI_SELECTION).equalsIgnoreCase("RANDOM")) {
            List uriList = Arrays.asList(metastoreUris);
            Collections.shuffle(uriList);
            metastoreUris = (URI[]) uriList.toArray();
        }
    } catch (IllegalArgumentException e) {
        throw (e);
    } catch (Exception e) {
        MetaStoreUtils.logAndThrowMetaException(e);
    }
}

From source file:org.sakaiproject.lessonbuildertool.service.LessonBuilderEntityProducer.java

/**
 * Takes a URL and then decides if it should be replaced.
 * //  w  w w  .  j av a2  s  . co  m
 * @param value
 * @return
 */
private String processUrl(ContentCopyContext context, String value, String contentUrl,
        Map<Long, Long> itemMap) {
    // Need to deal with backticks.
    // - /access/group/{siteId}/
    // - /web/{siteId}/
    // - /dav/{siteId}/
    // http(s)://weblearn.ox.ac.uk/ - needs trimming
    try {
        URI uri = new URI(value);
        uri = uri.normalize();
        if (value.startsWith(ITEMDUMMY)) {
            String num = value.substring(ITEMDUMMYLEN);
            int i = num.indexOf("/");
            if (i >= 0)
                num = num.substring(0, i);
            else
                return value;
            long oldItem = 0;
            try {
                oldItem = Long.parseLong(num);
            } catch (Exception e) {
                return value;
            }
            Long newItem = itemMap.get(oldItem);
            if (newItem == null)
                return value;
            return ITEMDUMMY + newItem + "/";
        } else if ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme())) {
            if (uri.getHost() != null) {
                // oldserver is the server that this archive is coming from
                // oldserver null means it's a local copy, e.g. duplicate site
                // for null we match URL against all of our server names
                String oldServer = context.getOldServer();
                if (oldServer == null && servers.contains(uri.getHost()) || uri.getHost().equals(oldServer)) {
                    // Drop the protocol and the host.
                    uri = new URI(null, null, null, -1, uri.getPath(), uri.getQuery(), uri.getFragment());
                }
            }
        }
        // Only do replacement on our URLs.
        if (uri.getHost() == null && uri.getPath() != null) {
            // Need to attempt todo path replacement now.
            String path = uri.getPath();
            Matcher matcher = pathPattern.matcher(path);

            if (matcher.matches() && context.getOldSiteId().equals(matcher.group(1))) {
                // Need to push the old URL onto the list of resources to
                // process. Except that we can't do that inside Lesson Builder
                //          addPath(context, path);
                String replacementPath = path.substring(0, matcher.start(1)) + context.getNewSiteId()
                        + path.substring(matcher.end(1));
                // Create a new URI with the new path
                uri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), replacementPath,
                        uri.getQuery(), uri.getFragment());
            } else if (!path.startsWith("/") && contentUrl != null) {
                // Relative URL.
                try {
                    URI base = new URI(contentUrl);
                    URI link = base.resolve(uri);
                    // sorry, no can do
                    //addPath(context, link.getPath());
                } catch (URISyntaxException e) {
                    System.err.println("Supplied contentUrl isn't valid: " + contentUrl);
                }
            }
        }
        return uri.toString();
    } catch (URISyntaxException e) {
        // Log this so we may get an idea of the things that are breaking
        // the parser.
        System.err.println("Failed to parse URL: " + value + " " + e.getMessage());
    }
    return value;
}

From source file:org.apache.hadoop.hive.ql.exec.DDLTask.java

private int archive(Hive db, AlterTableSimpleDesc simpleDesc, DriverContext driverContext)
        throws HiveException {

    Table tbl = db.getTable(simpleDesc.getTableName());

    if (tbl.getTableType() != TableType.MANAGED_TABLE) {
        throw new HiveException("ARCHIVE can only be performed on managed tables");
    }// w  w w .ja va 2 s. c o m

    Map<String, String> partSpec = simpleDesc.getPartSpec();
    PartSpecInfo partSpecInfo = PartSpecInfo.create(tbl, partSpec);
    List<Partition> partitions = db.getPartitions(tbl, partSpec);

    Path originalDir = null;

    // when we have partial partitions specification we must assume partitions
    // lie in standard place - if they were in custom locations putting
    // them into one archive would involve mass amount of copying
    // in full partition specification case we allow custom locations
    // to keep backward compatibility
    if (partitions.isEmpty()) {
        throw new HiveException("No partition matches the specification");
    } else if (partSpecInfo.values.size() != tbl.getPartCols().size()) {
        // for partial specifications we need partitions to follow the scheme
        for (Partition p : partitions) {
            if (partitionInCustomLocation(tbl, p)) {
                String message = String.format(
                        "ARCHIVE cannot run for partition " + "groups with custom locations like %s",
                        p.getLocation());
                throw new HiveException(message);
            }
        }
        originalDir = partSpecInfo.createPath(tbl);
    } else {
        Partition p = partitions.get(0);
        // partition can be archived if during recovery
        if (ArchiveUtils.isArchived(p)) {
            originalDir = new Path(getOriginalLocation(p));
        } else {
            originalDir = p.getDataLocation();
        }
    }

    Path intermediateArchivedDir = new Path(originalDir.getParent(),
            originalDir.getName() + INTERMEDIATE_ARCHIVED_DIR_SUFFIX);
    Path intermediateOriginalDir = new Path(originalDir.getParent(),
            originalDir.getName() + INTERMEDIATE_ORIGINAL_DIR_SUFFIX);

    console.printInfo("intermediate.archived is " + intermediateArchivedDir.toString());
    console.printInfo("intermediate.original is " + intermediateOriginalDir.toString());

    String archiveName = "data.har";
    FileSystem fs = null;
    try {
        fs = originalDir.getFileSystem(conf);
    } catch (IOException e) {
        throw new HiveException(e);
    }

    URI archiveUri = (new Path(originalDir, archiveName)).toUri();
    URI originalUri = ArchiveUtils.addSlash(originalDir.toUri());
    ArchiveUtils.HarPathHelper harHelper = new ArchiveUtils.HarPathHelper(conf, archiveUri, originalUri);

    // we checked if partitions matching specification are marked as archived
    // in the metadata; if they are and their levels are the same as we would
    // set it later it means previous run failed and we have to do the recovery;
    // if they are different, we throw an error
    for (Partition p : partitions) {
        if (ArchiveUtils.isArchived(p)) {
            if (ArchiveUtils.getArchivingLevel(p) != partSpecInfo.values.size()) {
                String name = ArchiveUtils.getPartialName(p, ArchiveUtils.getArchivingLevel(p));
                String m = String.format("Conflict with existing archive %s", name);
                throw new HiveException(m);
            } else {
                throw new HiveException("Partition(s) already archived");
            }
        }
    }

    boolean recovery = false;
    if (pathExists(intermediateArchivedDir) || pathExists(intermediateOriginalDir)) {
        recovery = true;
        console.printInfo("Starting recovery after failed ARCHIVE");
    }

    // The following steps seem roundabout, but they are meant to aid in
    // recovery if a failure occurs and to keep a consistent state in the FS

    // Steps:
    // 1. Create the archive in a temporary folder
    // 2. Move the archive dir to an intermediate dir that is in at the same
    //    dir as the original partition dir. Call the new dir
    //    intermediate-archive.
    // 3. Rename the original partition dir to an intermediate dir. Call the
    //    renamed dir intermediate-original
    // 4. Rename intermediate-archive to the original partition dir
    // 5. Change the metadata
    // 6. Delete the original partition files in intermediate-original

    // The original partition files are deleted after the metadata change
    // because the presence of those files are used to indicate whether
    // the original partition directory contains archived or unarchived files.

    // Create an archived version of the partition in a directory ending in
    // ARCHIVE_INTERMEDIATE_DIR_SUFFIX that's the same level as the partition,
    // if it does not already exist. If it does exist, we assume the dir is good
    // to use as the move operation that created it is atomic.
    if (!pathExists(intermediateArchivedDir) && !pathExists(intermediateOriginalDir)) {

        // First create the archive in a tmp dir so that if the job fails, the
        // bad files don't pollute the filesystem
        Path tmpPath = new Path(driverContext.getCtx().getExternalTmpPath(originalDir), "partlevel");

        console.printInfo("Creating " + archiveName + " for " + originalDir.toString());
        console.printInfo("in " + tmpPath);
        console.printInfo("Please wait... (this may take a while)");

        // Create the Hadoop archive
        int ret = 0;
        try {
            int maxJobNameLen = conf.getIntVar(HiveConf.ConfVars.HIVEJOBNAMELENGTH);
            String jobname = String.format("Archiving %s@%s", tbl.getTableName(), partSpecInfo.getName());
            jobname = Utilities.abbreviate(jobname, maxJobNameLen - 6);
            conf.set(MRJobConfig.JOB_NAME, jobname);
            HadoopArchives har = new HadoopArchives(conf);
            List<String> args = new ArrayList<String>();

            args.add("-archiveName");
            args.add(archiveName);
            args.add("-p");
            args.add(originalDir.toString());
            args.add(tmpPath.toString());

            ret = ToolRunner.run(har, args.toArray(new String[0]));
        } catch (Exception e) {
            throw new HiveException(e);
        }
        if (ret != 0) {
            throw new HiveException("Error while creating HAR");
        }

        // Move from the tmp dir to an intermediate directory, in the same level as
        // the partition directory. e.g. .../hr=12-intermediate-archived
        try {
            console.printInfo("Moving " + tmpPath + " to " + intermediateArchivedDir);
            if (pathExists(intermediateArchivedDir)) {
                throw new HiveException("The intermediate archive directory already exists.");
            }
            fs.rename(tmpPath, intermediateArchivedDir);
        } catch (IOException e) {
            throw new HiveException("Error while moving tmp directory");
        }
    } else {
        if (pathExists(intermediateArchivedDir)) {
            console.printInfo("Intermediate archive directory " + intermediateArchivedDir
                    + " already exists. Assuming it contains an archived version of the partition");
        }
    }

    // If we get to here, we know that we've archived the partition files, but
    // they may be in the original partition location, or in the intermediate
    // original dir.

    // Move the original parent directory to the intermediate original directory
    // if the move hasn't been made already
    if (!pathExists(intermediateOriginalDir)) {
        console.printInfo("Moving " + originalDir + " to " + intermediateOriginalDir);
        moveDir(fs, originalDir, intermediateOriginalDir);
    } else {
        console.printInfo(intermediateOriginalDir + " already exists. "
                + "Assuming it contains the original files in the partition");
    }

    // If there's a failure from here to when the metadata is updated,
    // there will be no data in the partition, or an error while trying to read
    // the partition (if the archive files have been moved to the original
    // partition directory.) But re-running the archive command will allow
    // recovery

    // Move the intermediate archived directory to the original parent directory
    if (!pathExists(originalDir)) {
        console.printInfo("Moving " + intermediateArchivedDir + " to " + originalDir);
        moveDir(fs, intermediateArchivedDir, originalDir);
    } else {
        console.printInfo(originalDir + " already exists. "
                + "Assuming it contains the archived version of the partition");
    }

    // Record this change in the metastore
    try {
        for (Partition p : partitions) {
            URI originalPartitionUri = ArchiveUtils.addSlash(p.getDataLocation().toUri());
            URI harPartitionDir = harHelper.getHarUri(originalPartitionUri);
            StringBuilder authority = new StringBuilder();
            if (harPartitionDir.getUserInfo() != null) {
                authority.append(harPartitionDir.getUserInfo()).append("@");
            }
            authority.append(harPartitionDir.getHost());
            if (harPartitionDir.getPort() != -1) {
                authority.append(":").append(harPartitionDir.getPort());
            }
            Path harPath = new Path(harPartitionDir.getScheme(), authority.toString(),
                    harPartitionDir.getPath()); // make in Path to ensure no slash at the end
            setArchived(p, harPath, partSpecInfo.values.size());
            db.alterPartition(simpleDesc.getTableName(), p, null);
        }
    } catch (Exception e) {
        throw new HiveException("Unable to change the partition info for HAR", e);
    }

    // If a failure occurs here, the directory containing the original files
    // will not be deleted. The user will run ARCHIVE again to clear this up
    if (pathExists(intermediateOriginalDir)) {
        deleteDir(intermediateOriginalDir);
    }

    if (recovery) {
        console.printInfo("Recovery after ARCHIVE succeeded");
    }

    return 0;
}

From source file:com.zimbra.client.ZMailbox.java

public static String resolveUrl(String url, boolean isAdmin) throws ZClientException {
    try {// w w w.j  ava  2 s. c o  m
        URI uri = new URI(url);

        if (isAdmin && uri.getPort() == -1) {
            uri = new URI("https", uri.getUserInfo(), uri.getHost(), ADMIN_PORT, uri.getPath(), uri.getQuery(),
                    uri.getFragment());
            url = uri.toString();
        }

        String service = (uri.getPort() == ADMIN_PORT) ? AdminConstants.ADMIN_SERVICE_URI
                : AccountConstants.USER_SERVICE_URI;
        if (uri.getPath() == null || uri.getPath().length() <= 1) {
            if (url.charAt(url.length() - 1) == '/') {
                url = url.substring(0, url.length() - 1) + service;
            } else {
                url = url + service;
            }
        }
        return url;
    } catch (URISyntaxException e) {
        throw ZClientException.CLIENT_ERROR("invalid URL: " + url, e);
    }
}