/*
* SoapUI, Copyright (C) 2004-2016 SmartBear Software
*
* Licensed under the EUPL, Version 1.1 or - as soon as they will be approved by the European Commission - subsequent
* versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under the Licence is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the Licence for the specific language governing permissions and limitations
* under the Licence.
*/
package com.eviware.soapui.impl.rest.actions.oauth;
import com.eviware.soapui.support.StringUtils;
import org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse;
import org.apache.oltu.oauth2.common.OAuth;
import org.apache.oltu.oauth2.common.error.OAuthError;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.token.BasicOAuthToken;
import org.apache.oltu.oauth2.common.token.OAuthToken;
import org.apache.oltu.oauth2.common.utils.JSONUtils;
import org.apache.oltu.oauth2.common.utils.OAuthUtils;
import org.codehaus.jettison.json.JSONException;
/**
*
*/
public class OAuth2AccessTokenResponse extends OAuthAccessTokenResponse {
private static final String EXPIRES = "expires";
@Override
public String getAccessToken() {
return getParam(OAuth.OAUTH_ACCESS_TOKEN);
}
@Override
public Long getExpiresIn() {
String value = getParam(OAuth.OAUTH_EXPIRES_IN) == null ? getParam(EXPIRES) : getParam(OAuth.OAUTH_EXPIRES_IN);
return value == null ? null : Long.valueOf(value);
}
public String getScope() {
return getParam(OAuth.OAUTH_SCOPE);
}
public OAuthToken getOAuthToken() {
return new BasicOAuthToken(getAccessToken(), getExpiresIn(), getRefreshToken(), getScope());
}
public String getRefreshToken() {
return getParam(OAuth.OAUTH_REFRESH_TOKEN);
}
protected void setBody(String body) throws OAuthProblemException {
try {
this.body = body;
parameters = JSONUtils.parseJSON(body);
} catch (JSONException e) {
if (body.startsWith("#")) {
body = body.substring(1);
}
parameters = OAuthUtils.decodeForm(body);
if (StringUtils.isNullOrEmpty(getAccessToken())) {
throw OAuthProblemException.error(OAuthError.CodeResponse.UNSUPPORTED_RESPONSE_TYPE,
"Invalid response! Response body is not " + OAuth.ContentType.JSON + " encoded or form-url-encoded");
}
}
}
protected void setContentType(String contentType) {
this.contentType = contentType;
}
protected void setResponseCode(int code) {
this.responseCode = code;
}
}