oauth.signpost.http.HttpRequest.getRequestUrl()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(14.4k)|赞(0)|评价(0)|浏览(93)

本文整理了Java中oauth.signpost.http.HttpRequest.getRequestUrl()方法的一些代码示例,展示了HttpRequest.getRequestUrl()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.getRequestUrl()方法的具体详情如下:
包路径:oauth.signpost.http.HttpRequest
类名称:HttpRequest
方法名:getRequestUrl

HttpRequest.getRequestUrl介绍

暂无

代码示例

代码示例来源:origin: mttkay/signpost

public String normalizeRequestUrl() throws URISyntaxException {
  URI uri = new URI(request.getRequestUrl());
  String scheme = uri.getScheme().toLowerCase();
  String authority = uri.getAuthority().toLowerCase();
  boolean dropPort = (scheme.equals("http") && uri.getPort() == 80)
      || (scheme.equals("https") && uri.getPort() == 443);
  if (dropPort) {
    // find the last : in the authority
    int index = authority.lastIndexOf(":");
    if (index >= 0) {
      authority = authority.substring(0, index);
    }
  }
  String path = uri.getRawPath();
  if (path == null || path.length() <= 0) {
    path = "/"; // conforms to RFC 2616 section 3.2.2
  }
  // we know that there is no query and no fragment here.
  return scheme + "://" + authority + path;
}

代码示例来源:origin: mttkay/signpost

/**
 * Collects HTTP GET query string parameters as per OAuth Core 1.0 spec
 * section 9.1.1
 */
protected void collectQueryParameters(HttpRequest request, HttpParameters out) {
  String url = request.getRequestUrl();
  int q = url.indexOf('?');
  if (q >= 0) {
    // Combine the URL query string with the other parameters:
    out.putAll(OAuth.decodeForm(url.substring(q + 1)), true);
  }
}

代码示例来源:origin: mttkay/signpost

public String writeSignature(String signature, HttpRequest request,
    HttpParameters requestParameters) {
  // add all (x_)oauth parameters
  HttpParameters oauthParams = requestParameters.getOAuthParameters();
  oauthParams.put(OAuth.OAUTH_SIGNATURE, signature, true);
  Iterator<String> iter = oauthParams.keySet().iterator();
  // add the first query parameter (we always have at least the signature)
  String firstKey = iter.next();
  StringBuilder sb = new StringBuilder(OAuth.addQueryString(request.getRequestUrl(),
    oauthParams.getAsQueryString(firstKey)));
  while (iter.hasNext()) {
    sb.append("&");
    String key = iter.next();
    sb.append(oauthParams.getAsQueryString(key));
  }
  String signedUrl = sb.toString();
  request.setRequestUrl(signedUrl);
  return signedUrl;
}

代码示例来源:origin: mttkay/signpost

public synchronized String sign(String url) throws OAuthMessageSignerException,
    OAuthExpectationFailedException, OAuthCommunicationException {
  HttpRequest request = new UrlStringRequestAdapter(url);
  // switch to URL signing
  SigningStrategy oldStrategy = this.signingStrategy;
  this.signingStrategy = new QueryStringSigningStrategy();
  sign(request);
  // revert to old strategy
  this.signingStrategy = oldStrategy;
  return request.getRequestUrl();
}

代码示例来源:origin: mttkay/signpost

@Before
  public void initRequestMocks() {
    MockitoAnnotations.initMocks(this);

    when(httpGetMock.getMethod()).thenReturn("GET");
    when(httpGetMock.getRequestUrl()).thenReturn("http://www.example.com");

    when(httpGetMockWithQueryString.getMethod()).thenReturn("GET");
    when(httpGetMockWithQueryString.getRequestUrl()).thenReturn("http://www.example.com?foo=bar");

    when(httpPostMock.getMethod()).thenReturn("POST");
    when(httpPostMock.getRequestUrl()).thenReturn("http://www.example.com");
  }
}

代码示例来源:origin: mttkay/signpost

@Test
public void shouldReturnCorrectRequestUrl() {
  assertEquals(URL, request.getRequestUrl());
}

代码示例来源:origin: mttkay/signpost

@Test
public void shouldEncodeAndConcatenateAllSignatureParts() throws Exception {
  HttpRequest request = mock(HttpRequest.class);
  when(request.getMethod()).thenReturn("GET");
  when(request.getRequestUrl()).thenReturn("http://example.com");
  HttpParameters params = new HttpParameters();
  params.put("a", "1");
  SignatureBaseString sbs = new SignatureBaseString(request, params);
  //TODO: Is it correct that a trailing slash is always added to the
  //request URL authority if the path is empty? 
  assertEquals("GET&http%3A%2F%2Fexample.com%2F&a%3D1", sbs.generate());
}

代码示例来源:origin: mttkay/signpost

@Test
public void shouldWorkWithBracketsInParameterName() throws Exception {
  HttpRequest request = mock(HttpRequest.class);
  when(request.getMethod()).thenReturn("GET");
  when(request.getRequestUrl()).thenReturn("http://examplebrackets.com");
  HttpParameters params = new HttpParameters();
  params.put("a[]", "1", true);
  SignatureBaseString sbs = new SignatureBaseString(request, params);
  
  assertEquals("GET&http%3A%2F%2Fexamplebrackets.com%2F&a%255B%255D%3D1", sbs.generate());
}

代码示例来源:origin: mttkay/signpost

@Test
public void shouldNormalizeRequestUrl() throws Exception {
  // must include scheme and authority in lowercase letters,
  // plus non HTTP(S) port, plus path,
  // but must ignore query params and fragment
  when(httpGetMock.getRequestUrl())
    .thenReturn("HTTP://www.Example.Com:123/test?q=1#fragment");
  assertEquals("http://www.example.com:123/test", new SignatureBaseString(httpGetMock,
    OAUTH_PARAMS).normalizeRequestUrl());
  // must exclude HTTP(S) default ports
  when(httpGetMock.getRequestUrl()).thenReturn("http://example.com:80");
  assertEquals("http://example.com/", new SignatureBaseString(httpGetMock, EMPTY_PARAMS)
    .normalizeRequestUrl());
  when(httpGetMock.getRequestUrl()).thenReturn("https://example.com:443");
  assertEquals("https://example.com/", new SignatureBaseString(httpGetMock, EMPTY_PARAMS)
    .normalizeRequestUrl());
}

代码示例来源:origin: mttkay/signpost

@Test
  public void shouldWorkWithMultipleParametersWithBracketsOfSameName() throws Exception {
    HttpRequest request = mock(HttpRequest.class);
    when(request.getMethod()).thenReturn("GET");
    when(request.getRequestUrl()).thenReturn("http://examplemultiple.com");

    HttpParameters params = new HttpParameters();
    params.put("a[]", "1", true);
    params.put("a[]", "2", true);

    SignatureBaseString sbs = new SignatureBaseString(request, params);
    
    assertEquals("GET&http%3A%2F%2Fexamplemultiple.com%2F&a%255B%255D%3D1%26a%255B%255D%3D2", sbs.generate());
  }
}

代码示例来源:origin: mttkay/signpost

@Test
public void shouldIncludeOAuthAndQueryAndBodyParams() throws Exception {
  // mock a request that has custom query, body, and header params set
  HttpRequest request = mock(HttpRequest.class);
  when(request.getRequestUrl()).thenReturn("http://example.com?a=1+1");
  ByteArrayInputStream body = new ByteArrayInputStream("b=2+2".getBytes());
  when(request.getMessagePayload()).thenReturn(body);
  when(request.getContentType()).thenReturn(
      "application/x-www-form-urlencoded; charset=ISO-8859-1");
  when(request.getHeader("Authorization")).thenReturn(
      "OAuth realm=\"http%3A%2F%2Fexample.com\", oauth_token=\"12%25345\", oauth_signature=\"1234\"");
  OAuthMessageSigner signer = mock(HmacSha1MessageSigner.class);
  consumer.setMessageSigner(signer);
  consumer.sign(request);
  // verify that all custom params are properly read and passed to the
  // message signer
  ArgumentMatcher<HttpParameters> hasAllParameters = new ArgumentMatcher<HttpParameters>() {
    public boolean matches(Object argument) {
      HttpParameters params = (HttpParameters) argument;
      assertEquals("1 1", params.getFirst("a", true));
      assertEquals("2 2", params.getFirst("b", true));
      assertEquals("http://example.com", params.getFirst("realm", true));
      assertEquals("12%345", params.getFirst("oauth_token", true));
      // signature should be dropped, not valid to pre-set
      assertNull(params.getFirst("oauth_signature"));
      return true;
    }
  };
  verify(signer).sign(same(request), argThat(hasAllParameters));
}

代码示例来源:origin: mttkay/signpost

@Test
public void shouldComputeCorrectHmacSha1Signature() throws Exception {
  // based on the reference test case from
  // http://oauth.pbwiki.com/TestCases
  OAuthMessageSigner signer = new HmacSha1MessageSigner();
  signer.setConsumerSecret(CONSUMER_SECRET);
  signer.setTokenSecret(TOKEN_SECRET);
  HttpRequest request = mock(HttpRequest.class);
  when(request.getRequestUrl()).thenReturn("http://photos.example.net/photos");
  when(request.getMethod()).thenReturn("GET");
  HttpParameters params = new HttpParameters();
  params.putAll(OAUTH_PARAMS);
  params.put("file", "vacation.jpg");
  params.put("size", "original");
  assertEquals("tR3+Ty81lMeYAr/Fid0kMTYa/WM=", signer.sign(request, params));
}

代码示例来源:origin: mttkay/signpost

@Test
  public void shouldComputeCorrectHmacSha256Signature() throws Exception {
    // based on the reference test case from
    // http://oauth.pbwiki.com/TestCases
    OAuthMessageSigner signer = new HmacSha256MessageSigner();
    signer.setConsumerSecret(CONSUMER_SECRET);
    signer.setTokenSecret(TOKEN_SECRET);

    HttpRequest request = mock(HttpRequest.class);
    when(request.getRequestUrl()).thenReturn("http://photos.example.net/photos");
    when(request.getMethod()).thenReturn("GET");

    HttpParameters params = new HttpParameters();
    params.putAll(OAUTH_PARAMS);
    params.put("file", "vacation.jpg");
    params.put("size", "original");

    assertEquals("0gCtTYQAxqCKhIE0sltgx7UgHkAs10vrpuYE7xpRBnE=", signer.sign(request, params));
  }
}

代码示例来源:origin: mttkay/signpost

@Test
public void shouldHonorManuallySetSigningParameters() throws Exception {
  // mock a request that has custom query, body, and header params set
  HttpRequest request = mock(HttpRequest.class);
  when(request.getRequestUrl()).thenReturn("http://example.com?a=1");
  OAuthMessageSigner signer = mock(HmacSha1MessageSigner.class);
  consumer.setMessageSigner(signer);
  HttpParameters params = new HttpParameters();
  params.put("oauth_callback", "http://mycallback");
  consumer.setAdditionalParameters(params);
  consumer.sign(request);
  // verify that all custom params are properly read and passed to the
  // message signer
  ArgumentMatcher<HttpParameters> hasParameters = new ArgumentMatcher<HttpParameters>() {
    public boolean matches(Object argument) {
      HttpParameters params = (HttpParameters) argument;
      assertEquals("http://mycallback", params.getFirst("oauth_callback"));
      assertEquals("1", params.getFirst("a"));
      return true;
    }
  };
  verify(signer).sign(same(request), argThat(hasParameters));
}

代码示例来源:origin: mttkay/signpost

public synchronized HttpRequest sign(HttpRequest request) throws OAuthMessageSignerException,
    OAuthExpectationFailedException, OAuthCommunicationException {
  if (consumerKey == null) {
    throw new OAuthExpectationFailedException("consumer key not set");
  }
  if (consumerSecret == null) {
    throw new OAuthExpectationFailedException("consumer secret not set");
  }
  requestParameters = new HttpParameters();
  try {
    if (additionalParameters != null) {
      requestParameters.putAll(additionalParameters, false);
    }
    collectHeaderParameters(request, requestParameters);
    collectQueryParameters(request, requestParameters);
    collectBodyParameters(request, requestParameters);
    // add any OAuth params that haven't already been set
    completeOAuthParameters(requestParameters);
    requestParameters.remove(OAuth.OAUTH_SIGNATURE);
  } catch (IOException e) {
    throw new OAuthCommunicationException(e);
  }
  String signature = messageSigner.sign(request, requestParameters);
  OAuth.debugOut("signature", signature);
  signingStrategy.writeSignature(signature, request, requestParameters);
  OAuth.debugOut("Request URL", request.getRequestUrl());
  return request;
}

代码示例来源:origin: oauth.signpost/signpost-core

public String normalizeRequestUrl() throws URISyntaxException {
  URI uri = new URI(request.getRequestUrl());
  String scheme = uri.getScheme().toLowerCase();
  String authority = uri.getAuthority().toLowerCase();
  boolean dropPort = (scheme.equals("http") && uri.getPort() == 80)
      || (scheme.equals("https") && uri.getPort() == 443);
  if (dropPort) {
    // find the last : in the authority
    int index = authority.lastIndexOf(":");
    if (index >= 0) {
      authority = authority.substring(0, index);
    }
  }
  String path = uri.getRawPath();
  if (path == null || path.length() <= 0) {
    path = "/"; // conforms to RFC 2616 section 3.2.2
  }
  // we know that there is no query and no fragment here.
  return scheme + "://" + authority + path;
}

代码示例来源:origin: oauth.signpost/signpost-core

/**
 * Collects HTTP GET query string parameters as per OAuth Core 1.0 spec
 * section 9.1.1
 */
protected void collectQueryParameters(HttpRequest request, HttpParameters out) {
  String url = request.getRequestUrl();
  int q = url.indexOf('?');
  if (q >= 0) {
    // Combine the URL query string with the other parameters:
    out.putAll(OAuth.decodeForm(url.substring(q + 1)), true);
  }
}

代码示例来源:origin: oauth.signpost/signpost-core

public String writeSignature(String signature, HttpRequest request,
    HttpParameters requestParameters) {
  // add all (x_)oauth parameters
  HttpParameters oauthParams = requestParameters.getOAuthParameters();
  oauthParams.put(OAuth.OAUTH_SIGNATURE, signature, true);
  Iterator<String> iter = oauthParams.keySet().iterator();
  // add the first query parameter (we always have at least the signature)
  String firstKey = iter.next();
  StringBuilder sb = new StringBuilder(OAuth.addQueryString(request.getRequestUrl(),
    oauthParams.getAsQueryString(firstKey)));
  while (iter.hasNext()) {
    sb.append("&");
    String key = iter.next();
    sb.append(oauthParams.getAsQueryString(key));
  }
  String signedUrl = sb.toString();
  request.setRequestUrl(signedUrl);
  return signedUrl;
}

代码示例来源:origin: oauth.signpost/signpost-core

public synchronized String sign(String url) throws OAuthMessageSignerException,
    OAuthExpectationFailedException, OAuthCommunicationException {
  HttpRequest request = new UrlStringRequestAdapter(url);
  // switch to URL signing
  SigningStrategy oldStrategy = this.signingStrategy;
  this.signingStrategy = new QueryStringSigningStrategy();
  sign(request);
  // revert to old strategy
  this.signingStrategy = oldStrategy;
  return request.getRequestUrl();
}

代码示例来源:origin: oauth.signpost/signpost-core

public synchronized HttpRequest sign(HttpRequest request) throws OAuthMessageSignerException,
    OAuthExpectationFailedException, OAuthCommunicationException {
  if (consumerKey == null) {
    throw new OAuthExpectationFailedException("consumer key not set");
  }
  if (consumerSecret == null) {
    throw new OAuthExpectationFailedException("consumer secret not set");
  }
  requestParameters = new HttpParameters();
  try {
    if (additionalParameters != null) {
      requestParameters.putAll(additionalParameters, false);
    }
    collectHeaderParameters(request, requestParameters);
    collectQueryParameters(request, requestParameters);
    collectBodyParameters(request, requestParameters);
    // add any OAuth params that haven't already been set
    completeOAuthParameters(requestParameters);
    requestParameters.remove(OAuth.OAUTH_SIGNATURE);
  } catch (IOException e) {
    throw new OAuthCommunicationException(e);
  }
  String signature = messageSigner.sign(request, requestParameters);
  OAuth.debugOut("signature", signature);
  signingStrategy.writeSignature(signature, request, requestParameters);
  OAuth.debugOut("Request URL", request.getRequestUrl());
  return request;
}

相关文章