本文整理汇总了Java中net.oauth.OAuth.formEncode方法的典型用法代码示例。如果您正苦于以下问题:Java OAuth.formEncode方法的具体用法?Java OAuth.formEncode怎么用?Java OAuth.formEncode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.oauth.OAuth
的用法示例。
在下文中一共展示了OAuth.formEncode方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendForm
import net.oauth.OAuth; //导入方法依赖的package包/类
/** Send the given parameters as a form-encoded response body. */
public static void sendForm(HttpServletResponse response,
Iterable<? extends Map.Entry> parameters) throws IOException {
response.resetBuffer();
response.setContentType(OAuth.FORM_ENCODED + ";charset="
+ OAuth.ENCODING);
OAuth.formEncode(parameters, response.getOutputStream());
}
示例2: normalizeParameters
import net.oauth.OAuth; //导入方法依赖的package包/类
@SuppressWarnings("rawtypes")
protected static String normalizeParameters(
Collection<? extends Map.Entry> parameters) throws IOException {
if (parameters == null) {
return "";
}
List<ComparableParameter> p = new ArrayList<ComparableParameter>(
parameters.size());
for (Map.Entry parameter : parameters) {
if (!"oauth_signature".equals(parameter.getKey())) {
p.add(new ComparableParameter(parameter));
}
}
Collections.sort(p);
return OAuth.formEncode(getParameters(p));
}
示例3: sendPostData
import net.oauth.OAuth; //导入方法依赖的package包/类
private String sendPostData(HttpMethod method) throws IOException {
String form;
if (useAuthHeader) {
form = nonOAuthParams.get(0).getValue();
} else {
form = OAuth.formEncode(message.getParameters());
}
method.addRequestHeader(HEADER_CONTENT_LENGTH, form.length() + ""); //$NON-NLS-1$
if (method instanceof PostMethod || method instanceof PutMethod) {
StringRequestEntity requestEntity = new StringRequestEntity(form);
((EntityEnclosingMethod)method).setRequestEntity(requestEntity);
} else {
log.error("Logic error, method must be POST or PUT to send body"); //$NON-NLS-1$
}
return form;
}
示例4: normalizeParameters
import net.oauth.OAuth; //导入方法依赖的package包/类
protected static String normalizeParameters(
Collection<? extends Map.Entry> parameters) throws IOException {
if (parameters == null) {
return "";
}
List<ComparableParameter> p = new ArrayList<ComparableParameter>(
parameters.size());
// oauth_signatureの元となる文字列を接続対象のアプリの認証ロジックにあわせて変更します
for (Map.Entry<String, String> parameter : parameters) {
// oauth_signatureの元となる文字列に、以下の形式でhttpのbodyを含める必要があります
// body={JSON形式のデータ}
// JMeterのデフォルトでは、以下の形式のため変更します
// ={JSON形式のデータ}
if ("".equals(parameter.getKey())){
OAuth.Parameter body = new OAuth.Parameter("body", parameter.getValue());
p.add(new ComparableParameter(body));
}
// oauth_signatureの元となる文字列に含めてはいけない文字列を除外します
else if (!"oauth_signature".equals(parameter.getKey()) &&
!"oauth_version".equals(parameter.getKey()) && !"auth_token".equals(parameter.getKey())) {
p.add(new ComparableParameter(parameter));
}
}
Collections.sort(p);
return OAuth.formEncode(getParameters(p));
}
示例5: sendPostData
import net.oauth.OAuth; //导入方法依赖的package包/类
private String sendPostData(HttpMethod method) throws IOException {
String form;
if (useAuthHeader) {
form = OAuth.formEncode(nonOAuthParams);
} else {
form = OAuth.formEncode(message.getParameters());
}
method.addRequestHeader(HEADER_CONTENT_TYPE, OAuth.FORM_ENCODED);
method.addRequestHeader(HEADER_CONTENT_LENGTH, form.length() + ""); //$NON-NLS-1$
if (method instanceof PostMethod || method instanceof PutMethod) {
StringRequestEntity requestEntity = new StringRequestEntity(
form, OAuth.FORM_ENCODED, OAuth.ENCODING);
((EntityEnclosingMethod)method).setRequestEntity(requestEntity);
} else {
log.error("Logic error, method must be POST or PUT to send body"); //$NON-NLS-1$
}
return form;
}
示例6: normalizeParameters
import net.oauth.OAuth; //导入方法依赖的package包/类
protected static String normalizeParameters(
Collection<? extends Map.Entry> parameters) throws IOException {
if (parameters == null) {
return "";
}
List<ComparableParameter> p = new ArrayList<ComparableParameter>(
parameters.size());
for (Map.Entry parameter : parameters) {
if (!"oauth_signature".equals(parameter.getKey())) {
p.add(new ComparableParameter(parameter));
}
}
Collections.sort(p);
return OAuth.formEncode(getParameters(p));
}
示例7: sendForm
import net.oauth.OAuth; //导入方法依赖的package包/类
/** Send the given parameters as a form-encoded response body. */
@SuppressWarnings("rawtypes")
public static void sendForm(HttpServletResponse response,
Iterable<? extends Map.Entry> parameters) throws IOException {
response.resetBuffer();
response.setContentType(OAuth.FORM_ENCODED + ";charset="
+ OAuth.ENCODING);
OAuth.formEncode(parameters, response.getOutputStream());
}
示例8: processRequest
import net.oauth.OAuth; //导入方法依赖的package包/类
public void processRequest(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try{
OAuthMessage requestMessage = OAuthServlet.getMessage(request, null);
OAuthAccessor accessor = SampleOAuthProvider.getAccessor(requestMessage);
SampleOAuthProvider.VALIDATOR.validateMessage(requestMessage, accessor);
// make sure token is authorized
if (!Boolean.TRUE.equals(accessor.getProperty("authorized"))) {
OAuthProblemException problem = new OAuthProblemException("permission_denied");
throw problem;
}
// generate access token and secret
SampleOAuthProvider.generateAccessToken(accessor);
response.setContentType("text/plain");
OutputStream out = response.getOutputStream();
OAuth.formEncode(OAuth.newList("oauth_token", accessor.accessToken,
"oauth_token_secret", accessor.tokenSecret),
out);
out.close();
} catch (Exception e){
SampleOAuthProvider.handleException(e, request, response, true);
}
}
示例9: processRequest
import net.oauth.OAuth; //导入方法依赖的package包/类
public void processRequest(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
OAuthMessage requestMessage = OAuthServlet.getMessage(request, null);
OAuthConsumer consumer = SampleOAuthProvider.getConsumer(requestMessage);
OAuthAccessor accessor = new OAuthAccessor(consumer);
SampleOAuthProvider.VALIDATOR.validateMessage(requestMessage, accessor);
{
// Support the 'Variable Accessor Secret' extension
// described in http://oauth.pbwiki.com/AccessorSecret
String secret = requestMessage.getParameter("oauth_accessor_secret");
if (secret != null) {
accessor.setProperty(OAuthConsumer.ACCESSOR_SECRET, secret);
}
}
// generate request_token and secret
SampleOAuthProvider.generateRequestToken(accessor);
response.setContentType("text/plain");
OutputStream out = response.getOutputStream();
OAuth.formEncode(OAuth.newList("oauth_token", accessor.requestToken,
"oauth_token_secret", accessor.tokenSecret),
out);
out.close();
} catch (Exception e){
SampleOAuthProvider.handleException(e, request, response, true);
}
}
示例10: formEncode
import net.oauth.OAuth; //导入方法依赖的package包/类
public static String formEncode(Iterable<? extends Entry<String, String>> parameters) {
try {
return OAuth.formEncode(parameters);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例11: sign
import net.oauth.OAuth; //导入方法依赖的package包/类
public FakeHttpServletRequest sign(String consumerKey, String consumerSecret, String requestor,
String token, String tokenSecret, OAuthParamLocation paramLocationEnum,
BodySigning bodySigning)
throws Exception {
FakeHttpServletRequest request = new FakeHttpServletRequest(url);
List<OAuth.Parameter> oauthParams = Lists.newArrayList();
UriBuilder target = new UriBuilder(Uri.parse(url));
String query = target.getQuery();
target.setQuery(null);
oauthParams.addAll(OAuth.decodeForm(query));
if (body != null) {
if (OAuth.isFormEncoded(contentType)) {
oauthParams.addAll(OAuth.decodeForm(body));
} else if (bodySigning == BodySigning.LEGACY) {
oauthParams.add(new OAuth.Parameter(body, ""));
} else if (bodySigning == BodySigning.HASH) {
oauthParams.add(
new OAuth.Parameter(OAuthConstants.OAUTH_BODY_HASH,
new String(Base64.encodeBase64(DigestUtils.sha(body.getBytes())), "UTF-8")));
}
}
oauthParams.add(new OAuth.Parameter(OAuth.OAUTH_CONSUMER_KEY, consumerKey));
oauthParams.add(new OAuth.Parameter("xoauth_requestor_id", requestor));
OAuthConsumer consumer = new OAuthConsumer(null,consumerKey,consumerSecret, null);
OAuthAccessor accessor = new OAuthAccessor(consumer);
if (!StringUtils.isEmpty(token)) {
accessor.accessToken = token;
accessor.tokenSecret = tokenSecret;
}
OAuthMessage message = accessor.newRequestMessage(method, target.toString(), oauthParams);
List<Map.Entry<String, String>> entryList = selectOAuthParams(message);
switch (paramLocationEnum) {
case AUTH_HEADER:
request.setHeader("Authorization", getAuthorizationHeader(entryList));
break;
case POST_BODY:
if (!OAuth.isFormEncoded(contentType)) {
throw new RuntimeException(
"OAuth param location can only be post_body if post body is of " +
"type x-www-form-urlencoded");
}
// All message params should be added if oauth params are added to body
for (Map.Entry<String, String> param : message.getParameters()) {
request.setParameter(param.getKey(), true, param.getValue());
}
String oauthData = OAuth.formEncode(message.getParameters());
request.setPostData(CharsetUtil.getUtf8Bytes(oauthData));
break;
case URI_QUERY:
request.setQueryString(Uri.parse(OAuth.addParameters(url, entryList)).getQuery());
break;
}
if (body != null && paramLocationEnum != OAuthParamLocation.POST_BODY) {
request.setContentType(contentType);
request.setPostData(body, "UTF-8");
if (contentType.contains(OAuth.FORM_ENCODED)) {
List<OAuth.Parameter> bodyParams = OAuth.decodeForm(body);
for (OAuth.Parameter bodyParam : bodyParams) {
request.setParameter(bodyParam.getKey(), bodyParam.getValue());
}
}
}
request.setMethod(method);
return request;
}
示例12: handleRequestTokenUrl
import net.oauth.OAuth; //导入方法依赖的package包/类
private HttpResponse handleRequestTokenUrl(HttpRequest request)
throws Exception {
MessageInfo info = parseMessage(request);
String requestConsumer = info.message.getParameter(OAuth.OAUTH_CONSUMER_KEY);
OAuthConsumer consumer;
if (CONSUMER_KEY.equals(requestConsumer)) {
consumer = oauthConsumer;
} else {
return makeOAuthProblemReport(
OAuthConstants.PROBLEM_CONSUMER_KEY_UNKNOWN, "invalid consumer: " + requestConsumer,
HttpResponse.SC_FORBIDDEN);
}
if (throttled) {
return makeOAuthProblemReport(
OAuthConstants.PROBLEM_CONSUMER_KEY_REFUSED, "exceeded quota exhausted",
HttpResponse.SC_FORBIDDEN);
}
if (unauthorized) {
return makeOAuthProblemReport(
OAuthConstants.PROBLEM_PERMISSION_DENIED, "user refused access",
HttpResponse.SC_BAD_REQUEST);
}
if (rejectExtraParams) {
String extra = hasExtraParams(info.message);
if (extra != null) {
return makeOAuthProblemReport(OAuthConstants.PROBLEM_PARAMETER_REJECTED, extra,
HttpResponse.SC_BAD_REQUEST);
}
}
OAuthAccessor accessor = new OAuthAccessor(consumer);
validateMessage(accessor, info, true);
String requestToken = Crypto.getRandomString(16);
String requestTokenSecret = Crypto.getRandomString(16);
String callbackUrl = info.message.getParameter(OAuth.OAUTH_CALLBACK);
tokenState.put(
requestToken, new TokenState(requestTokenSecret, accessor.consumer, callbackUrl));
List<Parameter> responseParams = OAuth.newList(
"oauth_token", requestToken,
"oauth_token_secret", requestTokenSecret);
if (callbackUrl != null) {
responseParams.add(new Parameter(OAuthConstants.OAUTH_CALLBACK_CONFIRMED, "true"));
}
return new HttpResponse(OAuth.formEncode(responseParams));
}
示例13: handleAccessTokenUrl
import net.oauth.OAuth; //导入方法依赖的package包/类
private HttpResponse handleAccessTokenUrl(HttpRequest request)
throws Exception {
MessageInfo info = parseMessage(request);
String requestToken = info.message.getParameter("oauth_token");
TokenState state = tokenState.get(requestToken);
if (throttled) {
return makeOAuthProblemReport(OAuthConstants.PROBLEM_CONSUMER_KEY_REFUSED,
"exceeded quota", HttpResponse.SC_FORBIDDEN);
} else if (unauthorized) {
return makeOAuthProblemReport(OAuthConstants.PROBLEM_PERMISSION_DENIED,
"user refused access", HttpResponse.SC_UNAUTHORIZED);
} else if (state == null) {
return makeOAuthProblemReport(OAuthConstants.PROBLEM_TOKEN_REJECTED,
"Unknown request token", HttpResponse.SC_UNAUTHORIZED);
}
if (rejectExtraParams) {
String extra = hasExtraParams(info.message);
if (extra != null) {
return makeOAuthProblemReport(OAuthConstants.PROBLEM_PARAMETER_REJECTED,
extra, HttpResponse.SC_BAD_REQUEST);
}
}
OAuthAccessor accessor = new OAuthAccessor(oauthConsumer);
accessor.requestToken = requestToken;
accessor.tokenSecret = state.tokenSecret;
validateMessage(accessor, info, true);
if (state.getState() == State.APPROVED_UNCLAIMED) {
String sentVerifier = info.message.getParameter("oauth_verifier");
if (state.verifier != null && !state.verifier.equals(sentVerifier)) {
return makeOAuthProblemReport(OAuthConstants.PROBLEM_BAD_VERIFIER, "wrong oauth verifier",
HttpResponse.SC_UNAUTHORIZED);
}
state.claimToken();
} else if (state.getState() == State.APPROVED) {
// Verify can refresh
String sentHandle = info.message.getParameter("oauth_session_handle");
if (sentHandle == null) {
return makeOAuthProblemReport(OAuthConstants.PROBLEM_PARAMETER_ABSENT,
"no oauth_session_handle", HttpResponse.SC_BAD_REQUEST);
}
if (!sentHandle.equals(state.sessionHandle)) {
return makeOAuthProblemReport(OAuthConstants.PROBLEM_TOKEN_INVALID, "token not valid",
HttpResponse.SC_UNAUTHORIZED);
}
state.renewToken();
} else if (state.getState() == State.REVOKED){
return makeOAuthProblemReport(OAuthConstants.PROBLEM_TOKEN_REVOKED,
"Revoked access token can't be renewed", HttpResponse.SC_UNAUTHORIZED);
} else {
throw new Exception("Token in weird state " + state.getState());
}
String accessToken = Crypto.getRandomString(16);
String accessTokenSecret = Crypto.getRandomString(16);
state.tokenSecret = accessTokenSecret;
tokenState.put(accessToken, state);
tokenState.remove(requestToken);
List<OAuth.Parameter> params = OAuth.newList(
"oauth_token", accessToken,
"oauth_token_secret", accessTokenSecret);
if (sessionExtension) {
params.add(new OAuth.Parameter("oauth_session_handle", state.sessionHandle));
if (reportExpirationTimes) {
params.add(new OAuth.Parameter("oauth_expires_in", "" + TOKEN_EXPIRATION_SECONDS));
}
}
if (returnAccessTokenData) {
params.add(new OAuth.Parameter("userid", "userid value"));
params.add(new OAuth.Parameter("xoauth_stuff", "xoauth_stuff value"));
params.add(new OAuth.Parameter("oauth_stuff", "oauth_stuff value"));
}
return new HttpResponse(OAuth.formEncode(params));
}