use of org.wso2.carbon.apimgt.api.APIManagementException in project product-apim by wso2.
the class TestUtil method generateClient.
public static DCRClientInfo generateClient() throws APIManagementException {
DCRClientInfo dcrClientInfo = new DCRClientInfo();
dcrClientInfo.setClientName("apim-integration-test");
dcrClientInfo.setGrantTypes(Arrays.asList(new String[] { "password", "client_credentials" }));
try {
Response response = DCRMServiceStubFactory.getDCRMServiceStub(DYNAMIC_CLIENT_REGISTRATION_ENDPOINT, username, password, "wso2carbon").registerApplication(dcrClientInfo);
DCRClientInfo dcrClientInfoResponse = (DCRClientInfo) new GsonDecoder().decode(response, DCRClientInfo.class);
clientId = dcrClientInfoResponse.getClientId();
clientSecret = dcrClientInfoResponse.getClientSecret();
return dcrClientInfoResponse;
} catch (APIManagementException | IOException e) {
logger.error("Couldn't create client", e);
throw new APIManagementException("Couldn't create client", e);
}
}
use of org.wso2.carbon.apimgt.api.APIManagementException in project product-apim by wso2.
the class TestUtil method generateToken.
private static void generateToken(String username, String password, String scopes) throws APIManagementException {
if (StringUtils.isEmpty(clientId) | StringUtils.isEmpty(clientSecret)) {
generateClient();
}
OAuth2ServiceStubs.TokenServiceStub tokenServiceStub = getOauth2Client();
Response response = tokenServiceStub.generatePasswordGrantAccessToken(username, password, scopes, -1, clientId, clientSecret);
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
// 200 - Success
logger.debug("A new access token is successfully generated.");
try {
OAuth2TokenInfo oAuth2TokenInfo = (OAuth2TokenInfo) new GsonDecoder().decode(response, OAuth2TokenInfo.class);
accessTokenInfo = new TokenInfo(oAuth2TokenInfo.getAccessToken(), System.currentTimeMillis() + oAuth2TokenInfo.getExpiresIn());
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing token response", e, ExceptionCodes.ACCESS_TOKEN_GENERATION_FAILED);
}
}
}
use of org.wso2.carbon.apimgt.api.APIManagementException in project carbon-apimgt by wso2.
the class APIPublisherImpl method removePendingLifecycleWorkflowTaskForAPI.
/**
* {@inheritDoc}
*/
@Override
public void removePendingLifecycleWorkflowTaskForAPI(String apiId) throws APIManagementException {
try {
API api = getApiDAO().getAPI(apiId);
if (APILCWorkflowStatus.PENDING.toString().equals(api.getWorkflowStatus())) {
// change the state back
getApiDAO().updateAPIWorkflowStatus(apiId, APILCWorkflowStatus.APPROVED);
// call executor's cleanup task
cleanupPendingTaskForAPIStateChange(apiId);
} else {
String msg = "API does not have a pending lifecycle state change.";
log.error(msg);
throw new APIManagementException(msg, ExceptionCodes.WORKFLOW_NO_PENDING_TASK);
}
} catch (APIMgtDAOException e) {
String msg = "Error occurred while changing api lifecycle workflow status";
log.error(msg, e);
throw new APIManagementException(msg, e.getErrorHandler());
}
}
use of org.wso2.carbon.apimgt.api.APIManagementException in project carbon-apimgt by wso2.
the class APIPublisherImpl method updateEndpoint.
/**
* Update and endpoint
*
* @param endpoint Endpoint object.
* @throws APIManagementException If failed to update endpoint.
*/
@Override
public void updateEndpoint(Endpoint endpoint) throws APIManagementException {
APIGateway gateway = getApiGateway();
gateway.updateEndpoint(endpoint);
String config = getGatewaySourceGenerator().getEndpointConfigStringFromTemplate(endpoint);
Endpoint updatedEndpoint = new Endpoint.Builder(endpoint).config(config).build();
try {
getApiDAO().updateEndpoint(updatedEndpoint);
} catch (APIMgtDAOException e) {
String msg = "Failed to update Endpoint : " + endpoint.getName();
log.error(msg, e);
throw new APIManagementException(msg, e, e.getErrorHandler());
}
// update endpoint config in gateway
}
use of org.wso2.carbon.apimgt.api.APIManagementException in project carbon-apimgt by wso2.
the class APIPublisherImpl method searchAPIs.
/**
* @param limit Limit
* @param offset Offset
* @param query Search query
* @return List of APIS.
* @throws APIManagementException If failed to formatApiSearch APIs.
*/
@Override
public List<API> searchAPIs(Integer limit, Integer offset, String query) throws APIManagementException {
List<API> apiResults;
String user = getUsername();
Set<String> roles = new HashSet<>();
try {
// TODO: Need to validate users roles against results returned
if (!"admin".equals(user)) {
// Whenever call identity provider should convert pseudo name to actual name
String userId = getIdentityProvider().getIdOfUser(user);
roles = new HashSet<>(getIdentityProvider().getRoleIdsOfUser(userId));
}
if (query != null && !query.isEmpty()) {
String[] attributes = query.split(ATTRIBUTE_DELIMITER);
Map<String, String> attributeMap = new HashMap<>();
boolean isFullTextSearch = false;
String searchAttribute, searchValue;
if (!query.contains(KEY_VALUE_DELIMITER)) {
isFullTextSearch = true;
} else {
log.debug("Search query: " + query);
for (String attribute : attributes) {
searchAttribute = attribute.split(KEY_VALUE_DELIMITER)[0];
searchValue = attribute.split(KEY_VALUE_DELIMITER)[1];
log.debug(searchAttribute + KEY_VALUE_DELIMITER + searchValue);
attributeMap.put(searchAttribute, searchValue);
}
}
if (isFullTextSearch) {
apiResults = getApiDAO().searchAPIs(roles, user, query, offset, limit);
} else {
log.debug("Attributes:", attributeMap.toString());
apiResults = getApiDAO().attributeSearchAPIs(roles, user, attributeMap, offset, limit);
}
} else {
apiResults = getApiDAO().getAPIs(roles, user);
}
return apiResults;
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while Searching the API with query " + query;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
} catch (IdentityProviderException e) {
String errorMsg = "Error occurred while calling SCIM endpoint to retrieve user " + user + "'s information";
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
Aggregations