Skip to content

Commit

Permalink
Add api support to try out branding AI feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sahandilshan committed Oct 2, 2024
1 parent a76694f commit 5ff3aec
Show file tree
Hide file tree
Showing 18 changed files with 1,027 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
</build>

<dependencies>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.utils</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.branding.preference.management</groupId>
<artifactId>org.wso2.carbon.identity.branding.preference.management.core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2023, WSO2 LLC. (http://www.wso2.com).
* Copyright (c) 2021-2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand Down Expand Up @@ -91,7 +91,13 @@ public enum ErrorMessage {
"Server encountered an error while deleting custom text configurations for organization: %s"),
ERROR_CODE_ERROR_UPDATING_CUSTOM_TEXT_PREFERENCE("65008",
"Unable to update custom text preference configurations.",
"Error while updating custom text preference configurations for organization: %s.");
"Error while updating custom text preference configurations for organization: %s."),
ERROR_CODE_ERROR_GETTING_BRANDING_RESULT_STATUS("65009",
"Error while getting branding preference generation result status.",
"Error while retrieving branding preference generation result status for operation."),
ERROR_CODE_ERROR_GETTING_BRANDING_RESULT("65010",
"Error while getting branding preference generation result.",
"Error while retrieving branding preference generation result for operation.");


private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
package org.wso2.carbon.identity.api.server.branding.preference.management.common;

import org.wso2.carbon.identity.branding.preference.management.core.BrandingPreferenceManager;
import org.wso2.carbon.identity.branding.preference.management.core.ai.BrandingAIPreferenceManager;

/**
* Service holder class for branding preference management.
*/
public class BrandingPreferenceServiceHolder {

private static BrandingPreferenceManager brandingPreferenceManager;
private static BrandingAIPreferenceManager brandingPreferenceAiManager;

/**
* Get BrandingPreferenceManager OSGi service.
Expand All @@ -46,4 +48,24 @@ public static void setBrandingPreferenceManager(BrandingPreferenceManager brandi

BrandingPreferenceServiceHolder.brandingPreferenceManager = brandingPreferenceManager;
}

/**
* Get AIBrandingPreferenceManager OSGi service.
*
* @return AI Branding Preference Manager.
*/
public static BrandingAIPreferenceManager getBrandingPreferenceAiManager() {

return brandingPreferenceAiManager;
}

/**
* Set AIBrandingPreferenceManager OSGi service.
*
* @param brandingPreferenceAIManager AI Branding Preference Manager.
*/
public static void setBrandingPreferenceAiManager(BrandingAIPreferenceManager brandingPreferenceAIManager) {

BrandingPreferenceServiceHolder.brandingPreferenceAiManager = brandingPreferenceAIManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.api.server.branding.preference.management.common.factory;

import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.branding.preference.management.core.ai.BrandingAIPreferenceManager;


/**
* Factory Beans serve as a factory for creating other beans within the IOC container. This factory bean is used to
* instantiate the AIBrandingPreferenceManager type of object inside the container.
*/
public class BrandingPreferenceAIMgtOSGiServiceFactory extends AbstractFactoryBean<BrandingAIPreferenceManager> {

private BrandingAIPreferenceManager brandingAiPreferenceManager;

@Override
public Class<?> getObjectType() {

return Object.class;
}

@Override
protected BrandingAIPreferenceManager createInstance() throws Exception {

if (this.brandingAiPreferenceManager == null) {
BrandingAIPreferenceManager taskOperationService = (BrandingAIPreferenceManager) PrivilegedCarbonContext.
getThreadLocalCarbonContext().getOSGiService(BrandingAIPreferenceManager.class, null);

if (taskOperationService != null) {
this.brandingAiPreferenceManager = taskOperationService;
} else {
throw new Exception("Unable to retrieve ConfigurationManager service.");
}
}
return this.brandingAiPreferenceManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,10 @@
<artifactId>org.wso2.carbon.identity.branding.preference.management.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.application.authentication.framework</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2024, WSO2 LLC. (http://www.wso2.com).
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -24,6 +24,10 @@
import java.io.InputStream;
import java.util.List;

import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.BrandingGenerationRequestModel;
import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.BrandingGenerationResponseModel;
import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.BrandingGenerationResultModel;
import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.BrandingGenerationStatusModel;
import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.BrandingPreferenceModel;
import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.CustomTextModel;
import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.Error;
Expand Down Expand Up @@ -141,6 +145,75 @@ public Response deleteCustomText( @Valid@ApiParam(value = "Type to filter the
return delegate.deleteCustomText(type, name, locale, screen );
}

@Valid
@POST
@Path("/generate")
@Consumes({ "application/json" })
@Produces({ "application/json" })
@ApiOperation(value = "Generate branding preferences using AI based on the organization's website.", notes = "This API endpoint initiates the generation of a new set of branding preferences by leveraging AI to analyze the organization's website. This is typically used when an organization wants to create branding preferences using AI. The endpoint requires a website URL and generates matching branding details based on the website's properties.<br> <b>Scope(Permission) required:</b> `internal_branding_preference_update` ", response = BrandingGenerationResponseModel.class, authorizations = {
@Authorization(value = "BasicAuth"),
@Authorization(value = "OAuth2", scopes = {

})
}, tags={ "Branding Preference", })
@ApiResponses(value = {
@ApiResponse(code = 202, message = "Branding generation process started", response = BrandingGenerationResponseModel.class),
@ApiResponse(code = 400, message = "Invalid input in the request.", response = Error.class),
@ApiResponse(code = 401, message = "Authentication information is missing or invalid.", response = Void.class),
@ApiResponse(code = 403, message = "Access forbidden.", response = Void.class),
@ApiResponse(code = 409, message = "Conflict.", response = Error.class),
@ApiResponse(code = 500, message = "Internal server error.", response = Error.class),
@ApiResponse(code = 501, message = "Not Implemented.", response = Error.class)
})
public Response generateBrandingPreference(@ApiParam(value = "This represents the properties of the organization used to generate branding preferences, including the organization's website URL." ,required=true) @Valid BrandingGenerationRequestModel brandingGenerationRequestModel) {

return delegate.generateBrandingPreference(brandingGenerationRequestModel );
}

@Valid
@GET
@Path("/result/{operationId}")

@Produces({ "application/json" })
@ApiOperation(value = "Return the result of a branding generation operation.", notes = "This API endpoint returns the result of an AI branding generation operation for a given operation ID. Depending on the operation status, the response may include an error message or the generated branding preferences.<br/> <b>Scope(Permission) required:</b> `internal_branding_preference_update` ", response = BrandingGenerationResultModel.class, authorizations = {
@Authorization(value = "BasicAuth"),
@Authorization(value = "OAuth2", scopes = {

})
}, tags={ "Branding Preference", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK", response = BrandingGenerationResultModel.class)
})
public Response getBrandingGenerationResult(@ApiParam(value = "The unique identifier for the branding generation operation.",required=true) @PathParam("operationId") String operationId) {

Response brandingGenerationResult = delegate.getBrandingGenerationResult(operationId);
return brandingGenerationResult;
}

@Valid
@GET
@Path("/status/{operationId}")

@Produces({ "application/json" })
@ApiOperation(value = "Get the status of a branding generation operation.", notes = "This API endpoint return the status of the AI branding generation process that initiated using the `/generate` endpoint.<br/> <b>Scope(Permission) required:</b> `internal_branding_preference_update` ", response = BrandingGenerationStatusModel.class, authorizations = {
@Authorization(value = "BasicAuth"),
@Authorization(value = "OAuth2", scopes = {

})
}, tags={ "Branding Preference", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK", response = BrandingGenerationStatusModel.class),
@ApiResponse(code = 400, message = "Invalid input in the request.", response = Error.class),
@ApiResponse(code = 401, message = "Authentication information is missing or invalid.", response = Void.class),
@ApiResponse(code = 403, message = "Access forbidden.", response = Void.class),
@ApiResponse(code = 404, message = "Requested resource is not found.", response = Error.class),
@ApiResponse(code = 500, message = "Internal server error.", response = Error.class)
})
public Response getBrandingGenerationStatus(@ApiParam(value = "The unique identifier for the branding generation operation.",required=true) @PathParam("operationId") String operationId) {

return delegate.getBrandingGenerationStatus(operationId );
}

@Valid
@GET

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2024, WSO2 LLC. (http://www.wso2.com).
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand All @@ -24,6 +24,10 @@
import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import java.io.InputStream;
import java.util.List;
import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.BrandingGenerationRequestModel;
import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.BrandingGenerationResponseModel;
import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.BrandingGenerationResultModel;
import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.BrandingGenerationStatusModel;
import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.BrandingPreferenceModel;
import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.CustomTextModel;
import org.wso2.carbon.identity.api.server.branding.preference.management.v1.model.Error;
Expand All @@ -40,6 +44,12 @@ public interface BrandingPreferenceApiService {

public Response deleteCustomText(String type, String name, String locale, String screen);

public Response generateBrandingPreference(BrandingGenerationRequestModel brandingGenerationRequestModel);

public Response getBrandingGenerationResult(String operationId);

public Response getBrandingGenerationStatus(String operationId);

public Response getBrandingPreference(String type, String name, String locale);

public Response getCustomText(String type, String name, String locale, String screen);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://www.wso2.com).
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.api.server.branding.preference.management.v1.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.*;


import io.swagger.annotations.*;
import java.util.Objects;
import javax.validation.Valid;
import javax.xml.bind.annotation.*;

public class BrandingGenerationRequestModel {

private String websiteUrl;

/**
* URL of the company&#39;s website.
**/
public BrandingGenerationRequestModel websiteUrl(String websiteUrl) {

this.websiteUrl = websiteUrl;
return this;
}

@ApiModelProperty(value = "URL of the company's website.")
@JsonProperty("website_url")
@Valid
public String getWebsiteUrl() {
return websiteUrl;
}
public void setWebsiteUrl(String websiteUrl) {
this.websiteUrl = websiteUrl;
}



@Override
public boolean equals(java.lang.Object o) {

if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BrandingGenerationRequestModel brandingGenerationRequestModel = (BrandingGenerationRequestModel) o;
return Objects.equals(this.websiteUrl, brandingGenerationRequestModel.websiteUrl);
}

@Override
public int hashCode() {
return Objects.hash(websiteUrl);
}

@Override
public String toString() {

StringBuilder sb = new StringBuilder();
sb.append("class BrandingGenerationRequestModel {\n");

sb.append(" websiteUrl: ").append(toIndentedString(websiteUrl)).append("\n");
sb.append("}");
return sb.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {

if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n");
}
}

Loading

0 comments on commit 5ff3aec

Please sign in to comment.