All Products
Search
Document Center

Content Moderation:Create a business scenario

Last Updated:Jul 31, 2023

This topic describes how to use Content Moderation SDK for Java to create a business scenario. You can use the business scenario to customize a policy for machine-assisted moderation for the Content Moderation API.

Prerequisites

  • Java dependencies are installed. For more information, see Installation.

    Note

    You must use the Java version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

  • The Extension.Uploader utility class is downloaded and imported into your project if you submit a local image or a binary image stream for image moderation.

Create a business scenario

Operation

Description

Supported region

CreateBizType

Creates a business scenario.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

Sample code

// Import the classes required to create a business scenario. 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20170823.CreateBizTypeRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * Use the following code to create a business scenario. 
 */
public class CreateBizTypeRequestSample {

    public static void main(String[] args) throws Exception {
        /**
         * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M.  
         * Common ways to obtain environment variables:
         * Method 1:
         *     Obtain the AccessKey ID of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * Method 2:
         *     Obtain the AccessKey ID of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
                "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        IAcsClient client = new DefaultAcsClient(profile);

        CreateBizTypeRequest createBizTypeRequest = new CreateBizTypeRequest();
        // Set the name of the business scenario that you want to create. 
        createBizTypeRequest.setBizTypeName("The name of the business scenario");
        // Import the configuration of an existing business scenario. This parameter is optional. 
        createBizTypeRequest.setBizTypeImport("The name of an existing business scenario to be imported");
        // Specify whether to import the configuration of an industry template. If you set this parameter to true, you must set the IndustryInfo parameter. This parameter is optional. 
        createBizTypeRequest.setCiteTemplate(true);
        // Specify the industry classification. This parameter is required if you set the CiteTemplate parameter to true. Valid values: Social-Registration information-Profile picture and Social-Registration information-Nickname. 
        createBizTypeRequest.setIndustryInfo("Social-Registration information-Profile picture");
        // Specify the response format of the operation. 
        createBizTypeRequest.setAcceptFormat(FormatType.JSON);
        // Specify the request method. 
        createBizTypeRequest.setMethod(com.aliyuncs.http.MethodType.POST);
        createBizTypeRequest.setEncoding("utf-8");
        // Specify the connection timeout period. You can change the timeout period as needed. Unit: milliseconds. 
        createBizTypeRequest.setConnectTimeout(3000);
        // Specify the read timeout period. You can change the timeout period as needed. Unit: milliseconds. 
        createBizTypeRequest.setReadTimeout(6000);

        // Send the HTTP request to create a business scenario. 
        try {
            HttpResponse httpResponse = client.doAction(createBizTypeRequest);
            // Check whether the HTTP request is successful. 
            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "utf-8"));
                System.out.println("create success." + JSON.toJSONString(scrResponse, true));
            } else {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "utf-8"));
                // Check the error message. 
                System.out.println("create not success. " + JSON.toJSONString(scrResponse.get("Message"), true));
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}