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:org.addhen.smssync.net.MainHttpClient.java

public MainHttpClient(String url, Context context) {

    this.url = url;
    this.context = context;
    this.params = new ArrayList<NameValuePair>();
    this.headers = new HashMap<String, String>();

    // default to GET
    this.method = "GET";
    request = new HttpGet(url);

    httpParameters = new BasicHttpParams();
    httpParameters.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 1);
    httpParameters.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(1));

    httpParameters.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
    HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(httpParameters, "utf8");
    // Set the timeout in milliseconds until a connection is established.
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);

    // in milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    SchemeRegistry schemeRegistry = new SchemeRegistry();

    // http scheme
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    // https scheme
    try {//from  w ww  .java  2s . co  m
        schemeRegistry.register(new Scheme("https", new TrustedSocketFactory(url, false), 443));
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(httpParameters, schemeRegistry);

    httpClient = new DefaultHttpClient(manager, httpParameters);

    // support basic auth header
    try {
        URI uri = new URI(url);
        String userInfo = uri.getUserInfo();
        if (userInfo != null) {
            setHeader("Authorization", "Basic " + base64Encode(userInfo));
        }
    } catch (URISyntaxException e) {
        debug(e);
    }

    // add user-agent header
    try {
        final String versionName = context.getPackageManager().getPackageInfo(context.getPackageName(),
                0).versionName;
        // Add version name to user agent
        userAgent = new StringBuilder("SMSSync-Android/");
        userAgent.append("v");
        userAgent.append(versionName);
        setHeader("User-Agent", userAgent.toString());
    } catch (NameNotFoundException e) {
        debug(e);
    }
}

From source file:com.almende.eve.transport.xmpp.XmppTransport.java

/**
 * Instantiates a new xmpp transport./*from w w  w. j a va 2  s .c  o m*/
 * 
 * @param config
 *            the config
 * @param handle
 *            the handle
 * @param service
 *            the service
 */
public XmppTransport(final XmppTransportConfig config, final Handler<Receiver> handle,
        final TransportService service) {
    // TODO: support more parameter structures.
    super(config.getAddress(), handle, service, config);

    final URI address = super.getAddress();
    host = address.getHost();
    port = address.getPort();
    if (port < 0) {
        port = 5222;
    }
    username = address.getUserInfo();
    resource = address.getPath().substring(1);
    if (serviceName == null) {
        serviceName = host;
    }
    password = config.getPassword();
}

From source file:org.addhen.smssync.net.BaseHttpClient.java

public BaseHttpClient(String url, Context context) {

    this.url = url;
    this.context = context;
    this.params = new ArrayList<>();
    this.headers = new HashMap<>();

    // default to GET
    this.method = HttpMethod.GET;
    request = new HttpGet(url);

    httpParameters = new BasicHttpParams();
    httpParameters.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 1);
    httpParameters.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(1));

    httpParameters.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
    HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(httpParameters, "utf8");
    // Set the timeout in milliseconds until a connection is established.
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);

    // in milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    SchemeRegistry schemeRegistry = new SchemeRegistry();

    // http scheme
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    // https scheme
    try {//w w w.ja  v a  2s.c  o m
        schemeRegistry.register(new Scheme("https", new TrustedSocketFactory(url, false), 443));
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(httpParameters, schemeRegistry);

    httpClient = new DefaultHttpClient(manager, httpParameters);

    // support basic auth header
    try {
        URI uri = new URI(url);
        String userInfo = uri.getUserInfo();
        if (userInfo != null) {
            setHeader("Authorization", "Basic " + base64Encode(userInfo));
        }
    } catch (URISyntaxException e) {
        debug(e);
    }

    // add user-agent header
    try {
        final String versionName = context.getPackageManager().getPackageInfo(context.getPackageName(),
                0).versionName;
        // Add version name to user agent
        userAgent = new StringBuilder("SMSSync-Android/");
        userAgent.append("v");
        userAgent.append(versionName);
        setHeader("User-Agent", userAgent.toString());
    } catch (NameNotFoundException e) {
        debug(e);
    }
}

From source file:com.gopivotal.cla.repository.RepositoryConfiguration.java

@Bean
DataSource dataSource() {//from   www .  j  a va2 s .c om
    URI dbUri = URI.create(this.databaseUrl);

    String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();

    BoneCPDataSource dataSource = new BoneCPDataSource();
    dataSource.setDriverClass(Driver.class.getCanonicalName());
    dataSource.setJdbcUrl(dbUrl);

    String[] userInfoTokens = dbUri.getUserInfo().split(":");
    dataSource.setUsername(userInfoTokens.length > 0 ? userInfoTokens[0] : "");
    dataSource.setPassword(userInfoTokens.length > 1 ? userInfoTokens[1] : "");

    dataSource.setMaxConnectionsPerPartition(2);

    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource);
    flyway.setLocations("META-INF/db/migration");
    flyway.migrate();

    return dataSource;
}

From source file:AuctionServlet.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        PreparedStatement stmt = null;

        try {/*ww  w .ja va  2s.c o m*/

            Class.forName("org.postgresql.Driver");
            URI dbUri = new URI(
                    "postgres://mvraljhmxcpilo:bKYSixo3rO1Z0cxAmyqMMcK7PG@ec2-75-101-162-243.compute-1.amazonaws.com:5432/d89kgd9u0h1bk3?username=mvraljhmxcpilo&password=bKYSixo3rO1Z0cxAmyqMMcK7PG&ssl.true&sslfactory=org.postgresql.ssl.NonValidatingFactory");

            String username = dbUri.getUserInfo().split(":")[0];
            String password = dbUri.getUserInfo().split(":")[1];
            String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();

            this.conn = DriverManager.getConnection(dbUrl, username, password);
            String sql;
            sql = "Select * \n" + " from BidData inner join CustomerData on \n"
                    + "BidData.CustomerID=CustomerData.CustomerID inner join \n"
                    + "ProductData on ProductData.ProductID=BidData.ProductID;";
            stmt = conn.prepareStatement(sql);
            ResultSet rs;
            rs = stmt.executeQuery(sql);
            JSONObject dataobj = new JSONObject();
            while (rs.next()) {
                dataobj.put("ProductID", rs.getInt("ProductID"));
                dataobj.put("ProductName", rs.getString("ProductName"));
                dataobj.put("SellerPrice", rs.getInt("SellerPrice"));
                dataobj.put("TimeLimit", rs.getInt("TimeLimit"));
                dataobj.put("ItemCondition", rs.getString("ItemCondition"));
                dataobj.put("DateSubmitted", rs.getDate("DateSubmitted"));
                dataobj.put("ProductPrice", rs.getInt("ProductPrice"));

            }
            out.println(dataobj);
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception E) {
            out.println(E.getMessage());
        }

    }
}

From source file:integration.util.graylog.ServerHelper.java

public String getNodeId() throws MalformedURLException, URISyntaxException {
    final URI uri = IntegrationTestsConfig.getGlServerURL();
    ObjectMapper mapper = new ObjectMapper();

    try {//from  w  w w  .  j  a  v a 2  s.  c o m
        HttpURLConnection connection = (HttpURLConnection) new URL(uri.toURL(), "/system").openConnection();
        connection.setConnectTimeout(HTTP_TIMEOUT);
        connection.setReadTimeout(HTTP_TIMEOUT);
        connection.setRequestMethod("GET");

        if (uri.getUserInfo() != null) {
            String encodedUserInfo = Base64
                    .encodeBase64String(uri.getUserInfo().getBytes(StandardCharsets.UTF_8));
            connection.setRequestProperty("Authorization", "Basic " + encodedUserInfo);
        }

        InputStream inputStream = connection.getInputStream();
        JsonNode json = mapper.readTree(inputStream);
        return json.get("node_id").toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "00000000-0000-0000-0000-000000000000";
}

From source file:org.eclipse.orion.server.git.objects.Status.java

private URI statusToFileLocation(URI u) throws URISyntaxException {
    String uriPath = u.getPath();
    String prefix = uriPath.substring(0, uriPath.indexOf(GitServlet.GIT_URI));
    uriPath = uriPath.substring(prefix.length() + (GitServlet.GIT_URI + '/' + Status.RESOURCE).length());
    return new URI(u.getScheme(), u.getUserInfo(), u.getHost(), u.getPort(), uriPath, u.getQuery(),
            u.getFragment());// w  w w.  j a  v a  2  s  .c  om
}

From source file:com.sina.cloudstorage.util.URIBuilder.java

private void digestURI(final URI uri) {
    this.scheme = uri.getScheme();
    this.schemeSpecificPart = uri.getSchemeSpecificPart();
    this.authority = uri.getAuthority();
    this.host = uri.getHost();
    this.port = uri.getPort();
    this.userInfo = uri.getUserInfo();
    this.path = uri.getPath();
    this.queryParams = parseQuery(uri.getRawQuery(), Consts.UTF_8);
    this.fragment = uri.getFragment();
}

From source file:io.jmnarloch.spring.cloud.discovery.DiscoveryClientPropertySource.java

/**
 * Expands the service URI./*from ww  w  .j  av  a 2 s.c om*/
 *
 * @param uri the input uri
 * @return the result uri
 */
private URI expandUri(String uri) {
    try {
        final URI inputUri = URI.create(uri);
        final ServiceInstance serviceInstance = findOneService(inputUri.getHost());
        if (serviceInstance == null) {
            return null;
        }
        return new URI((serviceInstance.isSecure() ? "https" : "http"), inputUri.getUserInfo(),
                serviceInstance.getHost(), serviceInstance.getPort(), inputUri.getPath(), inputUri.getQuery(),
                inputUri.getFragment());
    } catch (URISyntaxException e) {
        logger.error("Unexpected error occurred when expanding the property URI", e);
        throw new RuntimeException("Could not parse URI value: " + uri, e);
    }
}

From source file:rapture.common.connection.ConnectionInfoConfigurer.java

protected ConnectionInfo getConnectionInfoFromLocal(String instanceName) {
    try {//from   w  ww.j  a  va  2 s. com
        String url = getConnectionUrl(instanceName);
        URI uri = new URI(url);
        String host = StringUtils.isBlank(uri.getHost()) ? "localhost" : uri.getHost();
        int port = uri.getPort() < 0 ? getDefaultPort() : uri.getPort();
        String username = "";
        String password = "";
        if (!StringUtils.isBlank(uri.getUserInfo())) {
            String[] userInfo = uri.getUserInfo().split(":");
            username = userInfo[0];
            password = userInfo[1];
        }
        String dbName = uri.getPath().substring(1);
        ConnectionInfo c = new ConnectionInfo(host, port, username, password, dbName, instanceName);
        c.setUrl(url);
        return c;
    } catch (URISyntaxException e) {
        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Invalid URI", e);
    }
}