oauth.signpost.http.HttpRequest类的使用及代码示例

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

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

HttpRequest介绍

[英]A concise description of an HTTP request. Contains methods to access all those parts of an HTTP request which Signpost needs to sign a message. If you want to extend Signpost to sign a different kind of HTTP request than those currently supported, you'll have to write an adapter which implements this interface and a custom OAuthConsumer which performs the wrapping.
[中]HTTP请求的简明描述。包含访问HTTP请求中Signpost需要对消息进行签名的所有部分的方法。如果要扩展Signpost来签署与当前支持的HTTP请求不同的HTTP请求,则必须编写一个实现此接口的适配器和一个执行包装的自定义OAuthConsumer。

代码示例

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

@Override
  protected void closeConnection(HttpRequest request, HttpResponse response) {
    HttpURLConnection connection = (HttpURLConnection) request.unwrap();
    if (connection != null) {
      connection.disconnect();
    }
  }
}

代码示例来源: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

@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

/**
 * Collects x-www-form-urlencoded body parameters as per OAuth Core 1.0 spec
 * section 9.1.1
 */
protected void collectBodyParameters(HttpRequest request, HttpParameters out)
    throws IOException {
  // collect x-www-form-urlencoded body params
  String contentType = request.getContentType();
  if (contentType != null && contentType.startsWith(OAuth.FORM_ENCODED)) {
    InputStream payload = request.getMessagePayload();
    out.putAll(OAuth.decodeForm(payload), 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

/**
 * Collects OAuth Authorization header parameters as per OAuth Core 1.0 spec
 * section 9.1.1
 */
protected void collectHeaderParameters(HttpRequest request, HttpParameters out) {
  HttpParameters headerParams = OAuth.oauthHeaderToParamsMap(request.getHeader(OAuth.HTTP_AUTHORIZATION_HEADER));
  out.putAll(headerParams, false);
}

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

@Test
public void shouldGetAndSetRequestHeaders() {
  assertEquals(HEADER_VALUE, request.getHeader(HEADER_NAME));
  request.setHeader("a", "b");
  assertEquals("b", request.getHeader("a"));
  assertTrue(request.getAllHeaders().containsKey(HEADER_NAME));
  assertTrue(request.getAllHeaders().containsKey("a"));
}

代码示例来源: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

public String writeSignature(String signature, HttpRequest request,
    HttpParameters requestParameters) {
  StringBuilder sb = new StringBuilder();
  sb.append("OAuth ");
  // add the realm parameter, if any
  if (requestParameters.containsKey("realm")) {
    sb.append(requestParameters.getAsHeaderElement("realm"));
    sb.append(", ");
  }
  // add all (x_)oauth parameters
  HttpParameters oauthParams = requestParameters.getOAuthParameters();
  oauthParams.put(OAuth.OAUTH_SIGNATURE, signature, true);
  Iterator<String> iter = oauthParams.keySet().iterator();
  while (iter.hasNext()) {
    String key = iter.next();
    sb.append(oauthParams.getAsHeaderElement(key));
    if (iter.hasNext()) {
      sb.append(", ");
    }
  }
  String header = sb.toString();
  OAuth.debugOut("Auth Header", header);
  request.setHeader(OAuth.HTTP_AUTHORIZATION_HEADER, header);
  return header;
}

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

@Test
public void shouldReturnCorrectRequestMethod() {
  assertEquals(HTTP_POST_METHOD, request.getMethod());
}

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

@Test
  public void shouldReturnCorrectMessagePayload() throws Exception {
    String actual = new BufferedReader(new InputStreamReader(
        request.getMessagePayload())).readLine();
    assertEquals(PAYLOAD, actual);
  }
}

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

@Test
public void shouldReturnCorrectContentType() {
  assertEquals(CONTENT_TYPE, request.getContentType());
}

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

/**
 * Collects OAuth Authorization header parameters as per OAuth Core 1.0 spec
 * section 9.1.1
 */
protected void collectHeaderParameters(HttpRequest request, HttpParameters out) {
  HttpParameters headerParams = OAuth.oauthHeaderToParamsMap(request.getHeader(OAuth.HTTP_AUTHORIZATION_HEADER));
  out.putAll(headerParams, false);
}

代码示例来源: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: oauth.signpost/signpost-core

/**
 * Collects x-www-form-urlencoded body parameters as per OAuth Core 1.0 spec
 * section 9.1.1
 */
protected void collectBodyParameters(HttpRequest request, HttpParameters out)
    throws IOException {
  // collect x-www-form-urlencoded body params
  String contentType = request.getContentType();
  if (contentType != null && contentType.startsWith(OAuth.FORM_ENCODED)) {
    InputStream payload = request.getMessagePayload();
    out.putAll(OAuth.decodeForm(payload), true);
  }
}

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

ArgumentCaptor<String> arg2 = ArgumentCaptor.forClass(String.class);
verify(httpGetMock).setHeader(eq("Authorization"), arg1.capture());
verify(httpGetMockWithQueryString).setHeader(eq("Authorization"), arg2.capture());
HttpParameters headerMap1 = OAuth.oauthHeaderToParamsMap(arg1.getValue());
HttpParameters headerMap2 = OAuth.oauthHeaderToParamsMap(arg2.getValue());

代码示例来源: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: mttkay/signpost

/**
 * Builds the signature base string from the data this instance was
 * configured with.
 * 
 * @return the signature base string
 * @throws OAuthMessageSignerException
 */
public String generate() throws OAuthMessageSignerException {
  try {
    String normalizedUrl = normalizeRequestUrl();
    String normalizedParams = normalizeRequestParameters();
    return request.getMethod() + '&' + OAuth.percentEncode(normalizedUrl) + '&'
        + OAuth.percentEncode(normalizedParams);
  } catch (Exception e) {
    throw new OAuthMessageSignerException(e);
  }
}

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

protected HttpResponse sendRequest(HttpRequest request) throws IOException {
  HttpURLConnection connection = (HttpURLConnection) request.unwrap();
  connection.connect();
  return new HttpURLConnectionResponseAdapter(connection);
}

代码示例来源:origin: wso2-attic/esb-connectors

messageContext.setProperty("auth", response.getHeader(OAuth.HTTP_AUTHORIZATION_HEADER));

相关文章