Search in sources :

Example 36 with Patch

use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project carbon-apimgt by wso2.

the class SolaceAdminApis method applicationPatchRemoveSubscription.

/**
 * Remove subscriptions to application in Solace and update the application
 *
 * @param organization        name of the Organization
 * @param application         Application to be checked in solace
 * @param apiProductsToRemove List of API products to remove from subscriptions
 * @return CloseableHttpResponse of the PATCH call
 */
public CloseableHttpResponse applicationPatchRemoveSubscription(String organization, Application application, List<String> apiProductsToRemove) {
    URL serviceEndpointURL = new URL(baseUrl);
    HttpClient httpClient = APIUtil.getHttpClient(serviceEndpointURL.getPort(), serviceEndpointURL.getProtocol());
    HttpPatch httpPatch = new HttpPatch(baseUrl + "/" + organization + "/developers/" + developerUserName + "/apps/" + application.getUUID());
    httpPatch.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + getBase64EncodedCredentials());
    httpPatch.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
    // retrieve existing API products in the app
    ArrayList<String> apiProducts = new ArrayList<>();
    try {
        apiProducts = retrieveApiProductsInAnApplication(applicationGet(organization, application.getUUID(), "default"), apiProducts);
    } catch (IOException e) {
        log.error(e.getMessage());
    }
    // remove API product from arrayList
    apiProducts.removeAll(apiProductsToRemove);
    org.json.JSONObject requestBody = buildRequestBodyForApplicationPatchSubscriptions(apiProducts);
    StringEntity params = null;
    try {
        params = new StringEntity(requestBody.toString());
        httpPatch.setEntity(params);
        return APIUtil.executeHTTPRequest(httpPatch, httpClient);
    } catch (IOException | APIManagementException e) {
        log.error(e.getMessage());
    }
    return null;
}
Also used : StringEntity(org.apache.http.entity.StringEntity) JSONObject(org.json.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) HttpClient(org.apache.http.client.HttpClient) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(org.apache.axis2.util.URL) HttpPatch(org.apache.http.client.methods.HttpPatch)

Example 37 with Patch

use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project carbon-apimgt by wso2.

the class CORSRequestHandlerTestCase method setup.

@Before
public void setup() {
    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.when(APIUtil.getAllowedMethods()).thenReturn("GET,PUT,POST,DELETE,PATCH,OPTIONS");
    PowerMockito.when(APIUtil.isAllowCredentials()).thenReturn(false);
    PowerMockito.when(APIUtil.getAllowedOrigins()).thenReturn("*");
    PowerMockito.when(APIUtil.getAllowedHeaders()).thenReturn("authorization,Access-Control-Allow-Origin,Content-Type,SOAPAction");
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) Before(org.junit.Before)

Example 38 with Patch

use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project carbon-apimgt by wso2.

the class SolaceNotifierUtils method getSolaceAdminApis.

/**
 * Get and patch client id for Solace application
 *
 * @return SolaceAdminApis  object to invoke Solace
 * @throws APIManagementException If the Solace env configuration if not provided properly
 */
public static SolaceAdminApis getSolaceAdminApis() throws APIManagementException {
    Map<String, Environment> thirdPartyEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
    Environment solaceEnvironment = null;
    for (Map.Entry<String, Environment> entry : thirdPartyEnvironments.entrySet()) {
        if (SolaceConstants.SOLACE_ENVIRONMENT.equals(entry.getValue().getProvider())) {
            solaceEnvironment = entry.getValue();
        }
    }
    if (solaceEnvironment != null) {
        return new SolaceAdminApis(solaceEnvironment.getServerURL(), solaceEnvironment.getUserName(), solaceEnvironment.getPassword(), solaceEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_DEV_NAME));
    } else {
        throw new APIManagementException("Solace Environment configurations are not provided properly");
    }
}
Also used : SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Environment(org.wso2.carbon.apimgt.api.model.Environment) Map(java.util.Map)

Example 39 with Patch

use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project carbon-apimgt by wso2.

the class SolaceNotifierUtils method patchSolaceApplicationClientId.

/**
 * Get and patch client id for Solace application
 *
 * @param organization   Name of the Organization
 * @param application    Solace application
 * @param consumerKey    Consumer key to be used when patching
 * @param consumerSecret Consumer secret to be used when patching
 * @throws APIManagementException If the Solace env configuration if not provided properly
 */
public static void patchSolaceApplicationClientId(String organization, Application application, String consumerKey, String consumerSecret) throws APIManagementException {
    SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
    if (log.isDebugEnabled()) {
        log.info("Identified as Solace Application. Patching CliendID and Secret in solace application.....");
    }
    CloseableHttpResponse response = solaceAdminApis.patchClientIdForApplication(organization, application, consumerKey, consumerSecret);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        log.info("CliendID and Secret patched successfully for " + application.getName() + " Solace application");
    } else {
        log.error("Error while patching clientID for Solace application. : " + response.getStatusLine().toString());
    }
}
Also used : SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 40 with Patch

use of org.wso2.carbon.identity.api.server.idp.v1.model.Patch in project carbon-apimgt by wso2.

the class APIUtil method createSwaggerJSONContent.

/**
 * Create API Definition in JSON
 *
 * @param api API
 * @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to generate the content and save
 * @deprecated
 */
@Deprecated
public static String createSwaggerJSONContent(API api) throws APIManagementException {
    APIIdentifier identifier = api.getId();
    String organization = api.getOrganization();
    Environment environment = (Environment) getEnvironments(organization).values().toArray()[0];
    String endpoints = environment.getApiGatewayEndpoint();
    String[] endpointsSet = endpoints.split(",");
    String apiContext = api.getContext();
    String version = identifier.getVersion();
    Set<URITemplate> uriTemplates = api.getUriTemplates();
    String description = api.getDescription();
    // With the new context version strategy, the URL prefix is the apiContext. the verison will be embedded in
    // the apiContext.
    String urlPrefix = apiContext;
    if (endpointsSet.length < 1) {
        throw new APIManagementException("Error in creating JSON representation of the API" + identifier.getApiName());
    }
    if (description == null || "".equals(description)) {
        description = "";
    } else {
        description = description.trim();
    }
    Map<String, List<Operation>> uriTemplateDefinitions = new HashMap<String, List<Operation>>();
    List<APIResource> apis = new ArrayList<APIResource>();
    for (URITemplate template : uriTemplates) {
        List<Operation> ops;
        List<Parameter> parameters;
        String path = urlPrefix + APIUtil.removeAnySymbolFromUriTempate(template.getUriTemplate());
        /* path exists in uriTemplateDefinitions */
        if (uriTemplateDefinitions.get(path) != null) {
            ops = uriTemplateDefinitions.get(path);
            parameters = new ArrayList<Parameter>();
            String httpVerb = template.getHTTPVerb();
            /* For GET and DELETE Parameter name - Query Parameters */
            if (Constants.Configuration.HTTP_METHOD_GET.equals(httpVerb) || Constants.Configuration.HTTP_METHOD_DELETE.equals(httpVerb)) {
                Parameter queryParam = new Parameter(APIConstants.OperationParameter.QUERY_PARAM_NAME, APIConstants.OperationParameter.QUERY_PARAM_DESCRIPTION, APIConstants.OperationParameter.PAYLOAD_PARAM_TYPE, false, false, "String");
                parameters.add(queryParam);
            } else {
                /* For POST, PUT and PATCH Parameter name - Payload */
                Parameter payLoadParam = new Parameter(APIConstants.OperationParameter.PAYLOAD_PARAM_NAME, APIConstants.OperationParameter.PAYLOAD_PARAM_DESCRIPTION, APIConstants.OperationParameter.PAYLOAD_PARAM_TYPE, false, false, "String");
                parameters.add(payLoadParam);
            }
            Parameter authParam = new Parameter(APIConstants.OperationParameter.AUTH_PARAM_NAME, APIConstants.OperationParameter.AUTH_PARAM_DESCRIPTION, APIConstants.OperationParameter.AUTH_PARAM_TYPE, false, false, "String");
            parameters.add(authParam);
            if (!"OPTIONS".equals(httpVerb)) {
                Operation op = new Operation(httpVerb, description, description, parameters);
                ops.add(op);
            }
        } else {
            /* path not exists in uriTemplateDefinitions */
            ops = new ArrayList<Operation>();
            parameters = new ArrayList<Parameter>();
            String httpVerb = template.getHTTPVerb();
            /* For GET and DELETE Parameter name - Query Parameters */
            if (Constants.Configuration.HTTP_METHOD_GET.equals(httpVerb) || Constants.Configuration.HTTP_METHOD_DELETE.equals(httpVerb)) {
                Parameter queryParam = new Parameter(APIConstants.OperationParameter.QUERY_PARAM_NAME, APIConstants.OperationParameter.QUERY_PARAM_DESCRIPTION, APIConstants.OperationParameter.PAYLOAD_PARAM_TYPE, false, false, "String");
                parameters.add(queryParam);
            } else {
                /* For POST,PUT and PATCH Parameter name - Payload */
                Parameter payLoadParam = new Parameter(APIConstants.OperationParameter.PAYLOAD_PARAM_NAME, APIConstants.OperationParameter.PAYLOAD_PARAM_DESCRIPTION, APIConstants.OperationParameter.PAYLOAD_PARAM_TYPE, false, false, "String");
                parameters.add(payLoadParam);
            }
            Parameter authParam = new Parameter(APIConstants.OperationParameter.AUTH_PARAM_NAME, APIConstants.OperationParameter.AUTH_PARAM_DESCRIPTION, APIConstants.OperationParameter.AUTH_PARAM_TYPE, false, false, "String");
            parameters.add(authParam);
            if (!"OPTIONS".equals(httpVerb)) {
                Operation op = new Operation(httpVerb, description, description, parameters);
                ops.add(op);
            }
            uriTemplateDefinitions.put(path, ops);
        }
    }
    final Set<Entry<String, List<Operation>>> entries = uriTemplateDefinitions.entrySet();
    for (Entry entry : entries) {
        APIResource apiResource = new APIResource((String) entry.getKey(), description, (List<Operation>) entry.getValue());
        apis.add(apiResource);
    }
    APIDefinition apidefinition = new APIDefinition(version, APIConstants.SWAGGER_VERSION, endpointsSet[0], apiContext, apis);
    Gson gson = new Gson();
    return gson.toJson(apidefinition);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) Operation(org.wso2.carbon.apimgt.api.doc.model.Operation) Entry(java.util.Map.Entry) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIDefinition(org.wso2.carbon.apimgt.api.doc.model.APIDefinition) RecommendationEnvironment(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommendationEnvironment) Environment(org.wso2.carbon.apimgt.api.model.Environment) ExternalEnvironment(org.wso2.carbon.apimgt.impl.ExternalEnvironment) Parameter(org.wso2.carbon.apimgt.api.doc.model.Parameter) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)19 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)11 Test (org.testng.annotations.Test)11 JSONArray (org.json.JSONArray)9 JSONObject (org.json.JSONObject)9 Attribute (org.wso2.charon3.core.attributes.Attribute)9 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)9 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)9 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)9 CharonException (org.wso2.charon3.core.exceptions.CharonException)9 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)8 List (java.util.List)7 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)7 LinkedHashMap (java.util.LinkedHashMap)6 Map (java.util.Map)6 JSONException (org.json.JSONException)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 AttributeSchema (org.wso2.charon3.core.schema.AttributeSchema)6 ExtractableResponse (io.restassured.response.ExtractableResponse)5