public String getContextByPostMethod(String url, List<KeyValue> params) {
    HttpClient client = getHttpClient();

    client.getParams().setParameter("http.protocol.content-charset", this.codeing);

    PostMethod post = null;
    String result = "";
    try {
      URL u = new URL(url);
      client
          .getHostConfiguration()
          .setHost(
              u.getHost(), u.getPort() == -1 ? u.getDefaultPort() : u.getPort(), u.getProtocol());

      post = new PostMethod(u.getPath());

      NameValuePair[] nvps = new NameValuePair[params.size()];
      int i = 0;
      for (KeyValue kv : params) {
        nvps[i] = new NameValuePair(kv.getKey(), kv.getValue());
        i++;
      }

      post.setRequestBody(nvps);

      client.executeMethod(post);
      result = post.getResponseBodyAsString();
    } catch (Exception e) {
      throw new NetException("HttpClient catch!", e);
    } finally {
      if (post != null) post.releaseConnection();
    }
    return result;
  }
  private int getPort(URL url) {
    if (url.getPort() == -1) {
      return getDefaultPort(url.getProtocol());
    }

    return url.getPort();
  }
Esempio n. 3
0
  protected CloseableHttpClient constructHttpClient() throws IOException {
    RequestConfig config =
        RequestConfig.custom()
            .setConnectTimeout(20 * 1000)
            .setConnectionRequestTimeout(20 * 1000)
            .setSocketTimeout(20 * 1000)
            .setMaxRedirects(20)
            .build();

    URL mmsc = new URL(apn.getMmsc());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();

    if (apn.hasAuthentication()) {
      credsProvider.setCredentials(
          new AuthScope(
              mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
          new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
    }

    return HttpClients.custom()
        .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
        .setRedirectStrategy(new LaxRedirectStrategy())
        .setUserAgent("Android-Mms/2.0")
        .setConnectionManager(new BasicHttpClientConnectionManager())
        .setDefaultRequestConfig(config)
        .setDefaultCredentialsProvider(credsProvider)
        .build();
  }
  private String translateLocationUrl(
      URL locationUrl,
      URL proxiedHostUrl,
      URL requestedHost,
      String requestedContext,
      String proxiedRootPath) {
    StringBuilder buffer = new StringBuilder();

    if (locationUrl == null) {
      return null;
    }

    if (StringUtilities.isEmpty(locationUrl.getHost())) {
      return requestedContext;
    }

    if (shouldRewriteLocation(locationUrl, proxiedHostUrl, requestedHost)) {
      // location header contains our host info
      buffer.append(requestedHost.getProtocol()).append("://").append(requestedHost.getHost());
      if (requestedHost.getPort() != DEFAULT_HTTP_PORT) {
        buffer.append(":").append(requestedHost.getPort());
      }
      buffer.append(fixPathPrefix(locationUrl.getFile(), requestedContext, proxiedRootPath));
    }

    return buffer.length() == 0 ? locationUrl.toExternalForm() : buffer.toString();
  }
Esempio n. 5
0
  private String getBase(final String string) {
    if (string == null) {
      return "";
    }

    final String base = this.getRegex("<base\\s*href=\"(.*?)\"").getMatch(0);
    if (base != null) {
      return base;
    }

    final URL url = this.request.getHttpConnection().getURL();
    final String host = url.getHost();
    String portUse = "";
    if (url.getDefaultPort() > 0 && url.getPort() > 0 && url.getDefaultPort() != url.getPort()) {
      portUse = ":" + url.getPort();
    }
    String proto = "http://";
    if (url.toString().startsWith("https")) {
      proto = "https://";
    }
    String path = url.getPath();
    int id;
    if ((id = path.lastIndexOf('/')) >= 0) {
      path = path.substring(0, id);
    }
    return proto + host + portUse + path + "/";
  }
 public static HttpURLConnection getConnection(String urlStr) throws IOException {
   URL url = new URL(urlStr);
   NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
   if (null == activeNetworkInfo) {
     Log.w(TAG, "No active network available!");
     return null;
   }
   Proxy p = getProxy();
   String extra = activeNetworkInfo.getExtraInfo();
   // chinese mobile network
   if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE && p != null) {
     // cmwap, uniwap, 3gwap
     if (extra != null
         && (extra.startsWith("cmwap")
             || extra.startsWith("uniwap")
             || extra.startsWith("3gwap"))) {
       HttpURLConnection conn =
           (HttpURLConnection)
               new URL("http://" + p.address().toString() + url.getPath()).openConnection();
       conn.setRequestProperty(
           "X-Online-Host", url.getHost() + ":" + (url.getPort() == -1 ? "80" : url.getPort()));
       return conn;
     }
   }
   if (null != p) {
     // through proxy
     return (HttpURLConnection) url.openConnection(p);
   } else {
     return (HttpURLConnection) url.openConnection();
   }
 }
Esempio n. 7
0
  @Override
  public StatementsResult moreStatements(String moreURL) throws Exception {
    if (moreURL == null) {
      return null;
    }

    //
    // moreURL is relative to the endpoint's server root
    //
    URL endpoint = this.getEndpoint();
    String url =
        endpoint.getProtocol()
            + "://"
            + endpoint.getHost()
            + (endpoint.getPort() == -1 ? "" : endpoint.getPort())
            + moreURL;

    HTTPRequest request = new HTTPRequest();
    request.setURL(url);

    HTTPResponse response = this.sendRequest(request);
    if (response.getStatus() == 200) {
      return new StatementsResult(new StringOfJSON(response.getContent()));
    }

    throw new UnexpectedHTTPResponse(response);
  }
Esempio n. 8
0
 private static Registry getRegistry(URL u) throws RemoteException {
   if (u.getPort() == -1) {
     return (LocateRegistry.getRegistry(u.getHost()));
   } else {
     return (LocateRegistry.getRegistry(u.getHost(), u.getPort()));
   }
 }
Esempio n. 9
0
 /**
  * Comparison that does not consider Ref.
  *
  * @param url1 the url1
  * @param url2 the url2
  * @return true, if successful
  */
 public static boolean sameNoRefURL(URL url1, URL url2) {
   return Objects.equals(url1.getHost(), url2.getHost())
       && Objects.equals(url1.getProtocol(), url2.getProtocol())
       && (url1.getPort() == url2.getPort())
       && Objects.equals(url1.getFile(), url2.getFile())
       && Objects.equals(url1.getUserInfo(), url2.getUserInfo());
 }
Esempio n. 10
0
  public String signRequest(URL url, String method, byte[] body, String contentType)
      throws Exception {
    StringBuilder sb = new StringBuilder();

    sb.append(String.format("%s %s", method, url.getPath()));
    if (url.getQuery() != null) {
      sb.append(String.format("?%s", url.getQuery()));
    }

    sb.append(String.format("\nHost: %s", url.getHost()));
    if (url.getPort() > 0) {
      sb.append(String.format(":%d", url.getPort()));
    }

    if (contentType != null) {
      sb.append(String.format("\nContent-Type: %s", contentType));
    }

    // body
    sb.append("\n\n");
    if (incBody(body, contentType)) {
      sb.append(new String(body));
    }

    byte[] sum = HMac.HmacSHA1Encrypt(sb.toString(), this.secretKey);
    String sign = UrlSafeBase64.encodeToString(sum);
    return this.accessKey + ":" + sign;
  }
  public String getStudyHost(String studyOid) throws Exception {

    String ocUrl = CoreResources.getField("sysURL.base") + "rest2/openrosa/" + studyOid;
    String pManageUrl = CoreResources.getField("portalURL");
    String pManageUrlFull =
        pManageUrl + "/app/rest/oc/authorizations?studyoid=" + studyOid + "&instanceurl=" + ocUrl;

    CommonsClientHttpRequestFactory requestFactory = new CommonsClientHttpRequestFactory();
    requestFactory.setReadTimeout(PARTICIPATE_READ_TIMEOUT);
    RestTemplate rest = new RestTemplate(requestFactory);
    try {
      Authorization[] response = rest.getForObject(pManageUrlFull, Authorization[].class);
      if (response.length > 0
          && response[0].getStudy() != null
          && response[0].getStudy().getHost() != null
          && !response[0].getStudy().getHost().equals("")) {
        URL url = new URL(pManageUrl);
        String port = "";
        if (url.getPort() > 0) port = ":" + String.valueOf(url.getPort());
        return url.getProtocol()
            + "://"
            + response[0].getStudy().getHost()
            + "."
            + url.getHost()
            + port
            + "/#/login";
      }
    } catch (Exception e) {
      logger.error(e.getMessage());
      logger.error(ExceptionUtils.getStackTrace(e));
    }
    return "";
  }
Esempio n. 12
0
  /**
   * Generate the signature base that is used to produce the signature
   *
   * @param url The full url that needs to be signed including its non OAuth url parameters
   * @param httpMethod The http method used. Must be a valid HTTP method verb (POST,GET,PUT, etc)
   * @param parameters
   * @param normalizedUrl
   * @param normalizedRequestParameters
   * @return
   */
  private String generateSignatureBase(
      URL url,
      String httpMethod,
      List<QParameter> parameters,
      StringBuffer normalizedUrl,
      StringBuffer normalizedRequestParameters) {

    Collections.sort(parameters);

    normalizedUrl.append(url.getProtocol());
    normalizedUrl.append("://");
    normalizedUrl.append(url.getHost());
    if ((url.getProtocol().equals("http") || url.getProtocol().equals("https"))
        && url.getPort() != -1) {
      normalizedUrl.append(":");
      normalizedUrl.append(url.getPort());
    }
    normalizedUrl.append(url.getPath());
    normalizedRequestParameters.append(formEncodeParameters(parameters));

    StringBuffer signatureBase = new StringBuffer();
    signatureBase.append(httpMethod.toUpperCase());
    signatureBase.append("&");
    signatureBase.append(encode(normalizedUrl.toString()));
    signatureBase.append("&");
    signatureBase.append(encode(normalizedRequestParameters.toString()));
    return signatureBase.toString();
  }
  /**
   * checks if the passed reference to a SLD document is valid against the defined in the policy. If
   * <tt>user</ff> != <tt>null</tt> the valid sld reference addresses will be read from the
   * user/rights repository
   *
   * @param condition condition containing the definition of the valid sldRef
   * @param sldRef
   * @throws InvalidParameterValueException
   */
  private void validateSLD(Condition condition, URL sldRef) throws InvalidParameterValueException {

    OperationParameter op = condition.getOperationParameter(SLD);

    if (op == null && sldRef != null) {
      throw new InvalidParameterValueException(INVALIDSLD + sldRef);
    }

    // sldRef is valid because no restrictions are made
    if (sldRef == null || op.isAny()) return;

    List<String> list = op.getValues();
    String port = null;
    if (sldRef.getPort() != -1) {
      port = ":" + sldRef.getPort();
    } else {
      port = ":80";
    }
    String addr = sldRef.getProtocol() + "://" + sldRef.getHost() + port;
    if (!list.contains(addr)) {
      if (!op.isUserCoupled()) {
        throw new InvalidParameterValueException(INVALIDSLD + sldRef);
      }
      userCoupled = true;
    }

    try {
      SLDFactory.createSLD(sldRef);
    } catch (XMLParsingException e) {
      String s = org.deegree.i18n.Messages.getMessage("WMS_SLD_IS_NOT_VALID", sldRef);
      throw new InvalidParameterValueException(s);
    }
  }
  @Test
  public void testGetURLPrincipal() throws KettleDatabaseException, MalformedURLException {
    String testHostname = "testHostname";
    int port = 9429;
    String testDbName = "testDbName";
    impalaDatabaseMeta.getAttributes().put("principal", "testP");
    String urlString = impalaDatabaseMeta.getURL(testHostname, "" + port, testDbName);
    assertTrue(urlString.startsWith(ImpalaDatabaseMeta.URL_PREFIX));
    // Use known prefix
    urlString = "http://" + urlString.substring(ImpalaDatabaseMeta.URL_PREFIX.length());
    URL url = new URL(urlString);
    assertEquals(testHostname, url.getHost());
    assertEquals(port, url.getPort());
    assertEquals("/" + testDbName, url.getPath());

    impalaDatabaseMeta.getAttributes().remove("principal");
    impalaDatabaseMeta
        .getAttributes()
        .put(
            ImpalaDatabaseMeta.ATTRIBUTE_PREFIX_EXTRA_OPTION
                + impalaDatabaseMeta.getPluginId()
                + ".principal",
            "testP");
    urlString = impalaDatabaseMeta.getURL(testHostname, "" + port, testDbName);
    assertTrue(urlString.startsWith(ImpalaDatabaseMeta.URL_PREFIX));
    // Use known prefix
    urlString = "http://" + urlString.substring(ImpalaDatabaseMeta.URL_PREFIX.length());
    url = new URL(urlString);
    assertEquals(testHostname, url.getHost());
    assertEquals(port, url.getPort());
    assertEquals("/" + testDbName, url.getPath());
  }
Esempio n. 15
0
  public static String getEndpointFromUrl(URL baseUrl) {
    StringBuffer result = new StringBuffer();
    result.append(baseUrl.getProtocol()).append("://");
    result.append(baseUrl.getHost());
    if (baseUrl.getPort() > 0) result.append(':').append(baseUrl.getPort());

    return result.toString();
  }
 private CookieOrigin getCookieOrigin(HttpURLConnection connection) {
   URL url = connection.getURL();
   String path = url.getPath();
   if (null == path || path.trim().length() <= 0) {
     path = "/";
   }
   int port = url.getPort() > 0 ? url.getPort() : 0;
   return new CookieOrigin(url.getHost(), port, path, false);
 }
 /**
  * Sets a custom Sumo Logic API url, i.e., different from https://api.sumologic.com.
  *
  * @param urlString The custom sumo logic api URL.
  * @throws MalformedURLException On URL syntax error.
  */
 public void setURL(String urlString) throws MalformedURLException {
   URL url = new URL(urlString);
   this.hostname = url.getHost();
   this.port =
       (url.getPort() == -1)
           ? (url.getDefaultPort() == -1 ? 80 : url.getDefaultPort())
           : url.getPort();
   this.protocol = url.getProtocol();
 }
Esempio n. 18
0
 private String relativeStartsWithSlash(String baseURL, String relativeURL)
     throws MalformedURLException {
   URL url = new URL(baseURL);
   int port = url.getPort();
   return url.getProtocol()
       + "://"
       + url.getHost()
       + ((port == 80 || port == -1) ? "" : ":" + url.getPort())
       + relativeURL;
 }
 public static ValidationInfo validateServer(
     SonarQubeServer server, SonarQubeServerDialog dialog) {
   try {
     URL url = new URL(server.getUrl());
     if (url.getPort() != -1 && (url.getPort() < 0 || url.getPort() > 0xFFFF)) {
       return new ValidationInfo("Port out of range:" + url.getPort(), dialog.getUrlTextField());
     }
   } catch (MalformedURLException e) {
     return new ValidationInfo("Invalid URL: " + e.getMessage(), dialog.getUrlTextField());
   }
   return null;
 }
Esempio n. 20
0
  private String getBase() {
    StringBuffer b = new StringBuffer();
    b.append(url.getProtocol());
    b.append("://");
    b.append(url.getHost());
    if (url.getPort() != -1) {
      b.append(":");
      b.append(url.getPort());
    }
    b.append(url.getPath());

    return b.toString();
  }
Esempio n. 21
0
 public THttpClient(String url, HttpClient client) throws TTransportException {
   try {
     url_ = new URL(url);
     this.client = client;
     this.host =
         new HttpHost(
             url_.getHost(),
             -1 == url_.getPort() ? url_.getDefaultPort() : url_.getPort(),
             url_.getProtocol());
   } catch (IOException iox) {
     throw new TTransportException(iox);
   }
 }
 protected void updateURLComponents(String urlStr) {
   try {
     URL url = new URL(urlStr);
     String port = url.getPort() == -1 ? "" : ":" + url.getPort(); // $NON-NLS-1$ //$NON-NLS-2$
     fURLHost.setText(
         url.getProtocol() + "://" + url.getHost() + port + "/"); // $NON-NLS-1$ //$NON-NLS-2$
     if (url.getQuery() != null) {
       fURLPath.setText(url.getPath() + "?" + url.getQuery()); // $NON-NLS-1$
     } else {
       fURLPath.setText(url.getPath());
     }
   } catch (MalformedURLException e) {
     Logger.logException(e);
   }
 }
 public Request authenticate(Proxy paramProxy, Response paramResponse) {
   List localList = paramResponse.challenges();
   paramResponse = paramResponse.request();
   URL localURL = paramResponse.url();
   int i = 0;
   int j = localList.size();
   while (i < j) {
     Object localObject = (Challenge) localList.get(i);
     if ("Basic".equalsIgnoreCase(((Challenge) localObject).getScheme())) {
       localObject =
           java.net.Authenticator.requestPasswordAuthentication(
               localURL.getHost(),
               getConnectToInetAddress(paramProxy, localURL),
               localURL.getPort(),
               localURL.getProtocol(),
               ((Challenge) localObject).getRealm(),
               ((Challenge) localObject).getScheme(),
               localURL,
               Authenticator.RequestorType.SERVER);
       if (localObject != null) {
         paramProxy =
             Credentials.basic(
                 ((PasswordAuthentication) localObject).getUserName(),
                 new String(((PasswordAuthentication) localObject).getPassword()));
         return paramResponse.newBuilder().header("Authorization", paramProxy).build();
       }
     }
     i += 1;
   }
   return null;
 }
Esempio n. 24
0
  public static URL getURL(double latitude, double longitude, String output)
      throws MalformedURLException, IOException {
    StringBuilder stringBuilder = new StringBuilder(URL_PREFIX);
    stringBuilder.append("?");
    stringBuilder.append("output=" + output + "&");
    stringBuilder.append("location=" + latitude + "," + longitude + "&");
    stringBuilder.append("key=" + PRIVATE_KEY);

    URL url = new URL(stringBuilder.toString());

    System.out.println(String.format("getProtocol %s", url.getProtocol()));
    System.out.println(String.format("getHost %s", url.getHost()));
    System.out.println(String.format("getPath %s", url.getPath()));
    System.out.println(String.format("getPort %s", url.getPort()));
    System.out.println(String.format("getDefaultPort %s", url.getDefaultPort()));
    System.out.println(String.format("getQuery %s", url.getQuery()));
    System.out.println(String.format("getAuthority %s", url.getAuthority()));
    System.out.println(String.format("getRef %s", url.getRef()));
    System.out.println(String.format("getUserInfo %s", url.getUserInfo()));
    System.out.println(String.format("getFile %s", url.getFile()));
    System.out.println(String.format("getContent %s", url.getContent()));
    System.out.println(String.format("toExternalForm %s", url.toExternalForm()));
    System.out.println("---------------");

    return url;
  }
Esempio n. 25
0
  public static void setAZTracker(URL tracker_url, boolean az_tracker) {
    String key = tracker_url.getHost() + ":" + tracker_url.getPort();

    synchronized (az_trackers) {
      boolean changed = false;

      if (az_trackers.get(key) == null) {

        if (az_tracker) {

          az_trackers.put(key, new Long(SystemTime.getCurrentTime()));

          changed = true;
        }
      } else {

        if (!az_tracker) {

          if (az_trackers.remove(key) != null) {

            changed = true;
          }
        }
      }

      if (changed) {

        COConfigurationManager.setParameter("Tracker Client AZ Instances", az_trackers);
      }
    }
  }
Esempio n. 26
0
 private boolean processURL(URL url, String baseDir, StatusWindow status) throws IOException {
   if (processedLinks.contains(url)) {
     return false;
   } else {
     processedLinks.add(url);
   }
   URLConnection connection = url.openConnection();
   InputStream in = new BufferedInputStream(connection.getInputStream());
   ArrayList list = processPage(in, baseDir, url);
   if ((status != null) && (list.size() > 0)) {
     status.setMaximum(list.size());
   }
   for (int i = 0; i < list.size(); i++) {
     if (status != null) {
       status.setMessage(Utils.trimFileName(list.get(i).toString(), 40), i);
     }
     if ((!((String) list.get(i)).startsWith("RUN"))
         && (!((String) list.get(i)).startsWith("SAVE"))
         && (!((String) list.get(i)).startsWith("LOAD"))) {
       processURL(
           new URL(url.getProtocol(), url.getHost(), url.getPort(), (String) list.get(i)),
           baseDir,
           status);
     }
   }
   in.close();
   return true;
 }
Esempio n. 27
0
 private URL targetURL(URL base, String name) throws MalformedURLException {
   StringBuffer sb = new StringBuffer(base.getFile().length() + name.length());
   sb.append(base.getFile());
   sb.append(name);
   String file = sb.toString();
   return new URL(base.getProtocol(), base.getHost(), base.getPort(), file, null);
 }
Esempio n. 28
0
  public static void main(String[] args) throws MalformedURLException {
    // URLオブジェクトを生成
    URL url = new URL("http://www.example.com:80/search?q=Java");

    // プロトコルを取得
    String protocol = url.getProtocol(); // => "http"
    System.out.println(protocol);

    // ホスト名を取得
    String host = url.getHost(); // => "www.example.com"
    System.out.println(host);

    // ポート番号を取得 (URLがポート番号を含まない場合は-1)
    int port = url.getPort(); // => 80
    System.out.println(port);

    // ファイル名 (パス+クエリ文字列)を取得
    String file = url.getFile(); // => "/search?q=Java"
    System.out.println(file);

    // パスを取得
    String path = url.getPath(); // => "/search"
    System.out.println(path);

    // クエリ文字列を取得 (URLがクエリ文字列を含まない場合はnull)
    String query = url.getQuery(); // => "q=Java"
    System.out.println(query);
  }
 /*
  * Enabled aggressive block sorting
  */
 @Override
 public Request authenticate(Proxy object, Response object2) throws IOException {
   List<Challenge> list = object2.challenges();
   object2 = object2.request();
   URL uRL = object2.url();
   int n2 = 0;
   int n3 = list.size();
   while (n2 < n3) {
     Object object3 = list.get(n2);
     if ("Basic".equalsIgnoreCase(object3.getScheme())
         && (object3 =
                 java.net.Authenticator.requestPasswordAuthentication(
                     uRL.getHost(),
                     this.getConnectToInetAddress((Proxy) object, uRL),
                     uRL.getPort(),
                     uRL.getProtocol(),
                     object3.getRealm(),
                     object3.getScheme(),
                     uRL,
                     Authenticator.RequestorType.SERVER))
             != null) {
       object = Credentials.basic(object3.getUserName(), new String(object3.getPassword()));
       return object2.newBuilder().header("Authorization", (String) object).build();
     }
     ++n2;
   }
   return null;
 }
Esempio n. 30
0
  private static void setValues(
      URL url,
      HttpResponse<InputStream> jsonResponse,
      AbstractMap<String, String> localHeaders,
      Long responseTime) {
    generalInfo = new HashMap<>();

    int responseCode = jsonResponse.getStatus();

    generalInfo.put("Protocol", url.getProtocol());
    generalInfo.put("Authority", url.getAuthority());
    generalInfo.put("Host", url.getHost());
    generalInfo.put("Default Port", Integer.toString(url.getDefaultPort()));
    generalInfo.put("Port ", Integer.toString(url.getPort()));
    generalInfo.put("Path", url.getPath());
    generalInfo.put("Query", url.getQuery());
    generalInfo.put("Filename", url.getFile());
    generalInfo.put("Ref", url.getRef());

    responseValues.resetProperties();
    responseValues.setRequestHeaders(localHeaders);
    responseValues.setResponseHeaders(jsonResponse.getHeaders());
    responseValues.setGeneralInfo(generalInfo);
    responseValues.setResponseTime(responseTime);
    responseValues.setResponseCode(
        Integer.toString(responseCode) + " " + jsonResponse.getStatusText());
  }