use of org.wso2.carbon.apimgt.core.auth.dto.DCRError in project carbon-apimgt by wso2.
the class DefaultKeyManagerImpl method updateApplication.
@Override
public OAuthApplicationInfo updateApplication(OAuthApplicationInfo oAuthApplicationInfo) throws KeyManagementException {
if (log.isDebugEnabled()) {
log.debug("Updating OAuth2 application with : " + oAuthApplicationInfo.toString());
}
String applicationName = oAuthApplicationInfo.getClientName();
String keyType = (String) oAuthApplicationInfo.getParameter(KeyManagerConstants.APP_KEY_TYPE);
if (keyType != null) {
// Derive oauth2 app name based on key type and user input for app name
applicationName = applicationName + '_' + keyType;
}
DCRClientInfo dcrClientInfo = new DCRClientInfo();
dcrClientInfo.setClientName(applicationName);
dcrClientInfo.setClientId(oAuthApplicationInfo.getClientId());
dcrClientInfo.setClientSecret(oAuthApplicationInfo.getClientSecret());
dcrClientInfo.addCallbackUrl(oAuthApplicationInfo.getCallBackURL());
dcrClientInfo.setGrantTypes(oAuthApplicationInfo.getGrantTypes());
Response response = dcrmServiceStub.updateApplication(dcrClientInfo, dcrClientInfo.getClientId());
if (response == null) {
throw new KeyManagementException("Error occurred while updating DCR application. Response is null", ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
// 200 - Success
try {
OAuthApplicationInfo oAuthApplicationInfoResponse = getOAuthApplicationInfo(response);
// setting original parameter list
oAuthApplicationInfoResponse.setParameters(oAuthApplicationInfo.getParameters());
if (log.isDebugEnabled()) {
log.debug("OAuth2 application updated: " + oAuthApplicationInfoResponse.toString());
}
return oAuthApplicationInfoResponse;
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing the DCR application update response " + "message.", e, ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
} else if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_400_BAD_REQUEST) {
// 400 - Known Error
try {
DCRError error = (DCRError) new GsonDecoder().decode(response, DCRError.class);
throw new KeyManagementException("Error occurred while updating DCR application. Error: " + error.getError() + ". Error Description: " + error.getErrorDescription() + ". Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing the DCR error message.", e, ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
} else {
// Unknown Error
throw new KeyManagementException("Error occurred while updating DCR application. Error: " + response.body().toString() + " Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_UPDATE_FAILED);
}
}
use of org.wso2.carbon.apimgt.core.auth.dto.DCRError in project carbon-apimgt by wso2.
the class DefaultKeyManagerImpl method createApplication.
@Override
public OAuthApplicationInfo createApplication(OAuthAppRequest oauthAppRequest) throws KeyManagementException {
log.debug("Creating OAuth2 application:{}", oauthAppRequest.toString());
String applicationName = oauthAppRequest.getClientName();
String keyType = oauthAppRequest.getKeyType();
if (keyType != null) {
// Derive oauth2 app name based on key type and user input for app name
applicationName = applicationName + '_' + keyType;
}
DCRClientInfo dcrClientInfo = new DCRClientInfo();
dcrClientInfo.setClientName(applicationName);
dcrClientInfo.setGrantTypes(oauthAppRequest.getGrantTypes());
if (StringUtils.isNotEmpty(oauthAppRequest.getCallBackURL())) {
dcrClientInfo.addCallbackUrl(oauthAppRequest.getCallBackURL());
}
Response response = dcrmServiceStub.registerApplication(dcrClientInfo);
if (response == null) {
throw new KeyManagementException("Error occurred while DCR application creation. Response is null", ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
}
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_201_CREATED) {
// 201 - Success
try {
OAuthApplicationInfo oAuthApplicationInfoResponse = getOAuthApplicationInfo(response);
// setting original parameter list
oAuthApplicationInfoResponse.setParameters(oauthAppRequest.getParameters());
log.debug("OAuth2 application created: {}", oAuthApplicationInfoResponse.toString());
return oAuthApplicationInfoResponse;
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing the DCR application creation response " + "message.", e, ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
}
} else if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_400_BAD_REQUEST) {
// 400 - Known Error
try {
DCRError error = (DCRError) new GsonDecoder().decode(response, DCRError.class);
throw new KeyManagementException("Error occurred while DCR application creation. Error: " + error.getError() + ". Error Description: " + error.getErrorDescription() + ". Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing the DCR error message.", e, ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
}
} else {
// Unknown Error
throw new KeyManagementException("Error occurred while DCR application creation. Error: " + response.body().toString() + " Status Code: " + response.status(), ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
}
}
Aggregations