Skip to content

Commit b923ee4

Browse files
committed
Implemented token authentication for chat and generate api payload resources
1 parent 246c962 commit b923ee4

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
2+
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
33
*
4-
* WSO2 Inc. licenses this file to you under the Apache License,
4+
* WSO2 LLC. licenses this file to you under the Apache License,
55
* Version 2.0 (the "License"); you may not use this file except
66
* in compliance with the License.
77
* You may obtain a copy of the License at
@@ -557,7 +557,6 @@ public static class AI {
557557
public static final String DESIGN_ASSISTANT_TOKEN_ENDPOINT = "TokenEndpoint";
558558
public static final String DESIGN_ASSISTANT_CHAT_RESOURCE = "ChatResource";
559559
public static final String DESIGN_ASSISTANT_GEN_API_PAYLOAD_RESOURCE = "GenApiPayloadResource";
560-
public static final String DESIGN_ASSISTANT_CONFIGURATION = "AiConfiguration";
561560

562561
private AI() {
563562

components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/DesignAssistantApiServiceImpl.java

+14-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package org.wso2.carbon.apimgt.rest.api.publisher.v1.impl;
1919

20+
import com.fasterxml.jackson.core.JsonProcessingException;
21+
import com.fasterxml.jackson.databind.JsonMappingException;
2022
import com.fasterxml.jackson.databind.ObjectMapper;
2123
import com.google.gson.Gson;
2224
import org.apache.commons.lang3.StringUtils;
@@ -52,7 +54,6 @@
5254
public class DesignAssistantApiServiceImpl implements DesignAssistantApiService {
5355

5456
private static final Log log = LogFactory.getLog(DesignAssistantApiServiceImpl.class);
55-
5657
private static DesignAssistantConfigurationDTO configDto;
5758

5859
@Override
@@ -84,12 +85,13 @@ public Response designAssistantApiPayloadGen(DesignAssistantGenAPIPayloadDTO des
8485
if (configDto.isKeyProvided()) {
8586
response = APIUtil.invokeAIService(configDto.getEndpoint(), configDto.getTokenEndpoint(),
8687
configDto.getKey(), configDto.getGenApiPayloadResource(), payload.toString(), null);
88+
8789
} else {
8890
response = APIUtil.invokeAIService(configDto.getEndpoint(), null,
8991
configDto.getAccessToken(), configDto.getGenApiPayloadResource(), payload.toString(), null);
92+
9093
}
9194

92-
ObjectMapper objectMapper = new ObjectMapper();
9395
DesignAssistantAPIPayloadResponseDTO responseDTO = new DesignAssistantAPIPayloadResponseDTO();
9496
responseDTO.setGeneratedPayload(response);
9597

@@ -122,35 +124,34 @@ public Response designAssistantChat(DesignAssistantChatQueryDTO designAssistantC
122124
}
123125
try {
124126
if (configDto.isKeyProvided() || configDto.isAuthTokenProvided()) {
125-
126-
boolean isChatQueryEmpty = StringUtils.isEmpty(designAssistantChatQueryDTO.getSessionId());
127+
String sessionId = designAssistantChatQueryDTO.getSessionId();
128+
boolean isChatQueryEmpty = StringUtils.isEmpty(sessionId);
127129
if (isChatQueryEmpty) {
128130
String errorMessage = "Payload is badly formatted. Expected to have 'sessionId'";
129131
RestApiUtil.handleBadRequest(errorMessage, log);
130132
return null;
131133
}
132134

133135
JSONObject payload = new JSONObject();
134-
String text = new Gson().toJson(designAssistantChatQueryDTO.getText());
135-
String sessionId = new Gson().toJson(designAssistantChatQueryDTO.getSessionId());
136136

137-
payload.put(APIConstants.QUERY, designAssistantChatQueryDTO.getSessionId());
138-
payload.put(APIConstants.SESSIONID, designAssistantChatQueryDTO.getSessionId());
137+
payload.put(APIConstants.TEXT, designAssistantChatQueryDTO.getText());
138+
payload.put(APIConstants.SESSIONID, sessionId);
139139

140140
String response;
141141
if (configDto.isKeyProvided()) {
142142
response = APIUtil.invokeAIService(configDto.getEndpoint(), configDto.getTokenEndpoint(),
143143
configDto.getKey(), configDto.getChatResource(), payload.toString(), null);
144+
144145
} else {
145146
response = APIUtil.invokeAIService(configDto.getEndpoint(), null,
146147
configDto.getAccessToken(), configDto.getChatResource(), payload.toString(), null);
148+
147149
}
148-
150+
149151
ObjectMapper objectMapper = new ObjectMapper();
150152
DesignAssistantChatResponseDTO responseDTO = objectMapper.readValue(response, DesignAssistantChatResponseDTO.class);
151153

152154
return Response.ok(responseDTO).build();
153-
154155
}
155156
} catch (APIManagementException e) {
156157
if (RestApiUtil.isDueToAIServiceNotAccessible(e)) {
@@ -162,7 +163,9 @@ public Response designAssistantChat(DesignAssistantChatQueryDTO designAssistantC
162163
"Assistant service";
163164
RestApiUtil.handleInternalServerError(errorMessage, e, log);
164165
}
166+
} catch (JsonProcessingException e) {
167+
throw new RuntimeException(e);
165168
}
166169
return null;
167170
}
168-
}
171+
}

0 commit comments

Comments
 (0)