Skip to content

Commit

Permalink
ACS-8121: call hxi question api (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawel-608 authored Jun 28, 2024
1 parent d0e6674 commit 605df12
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"questionId": "5fca2c77-cdc0-4118-9373-e75f53177ff8"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"request": {
"method": "POST",
"urlPath": "/v1/questions"
},
"response": {
"status": 202,
"bodyFileName": "question-id.json"
}
}
1 change: 1 addition & 0 deletions prediction-applier-extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
<include>org.alfresco:alfresco-hxinsight-connector-common</include>
<include>org.alfresco:alfresco-hxinsight-connector-common-authentication</include>
<include>org.apache.httpcomponents.core5:httpcore5</include>
<include>org.apache.httpcomponents.client5:httpclient5</include>
</includes>
</artifactSet>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,37 @@

import java.util.List;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;

import org.alfresco.hxi_connector.hxi_extension.rest.api.model.Question;
import org.alfresco.hxi_connector.hxi_extension.rest.api.model.QuestionModel;
import org.alfresco.hxi_connector.hxi_extension.service.HxInsightClient;
import org.alfresco.rest.framework.WebApiDescription;
import org.alfresco.rest.framework.resource.EntityResource;
import org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction;
import org.alfresco.rest.framework.resource.parameters.Parameters;

@Slf4j
@RequiredArgsConstructor
@EntityResource(name = "questions", title = "Questions about documents")
public class QuestionsEntityResource implements EntityResourceAction.Create<Question>
public class QuestionsEntityResource implements EntityResourceAction.Create<QuestionModel>
{
private final HxInsightClient hxInsightClient;

@Override
@WebApiDescription(title = "Ask question", successStatus = Status.STATUS_OK)
public List<Question> create(List<Question> questions, Parameters parameters)
public List<QuestionModel> create(List<QuestionModel> questions, Parameters parameters)
{
ensureThat(questions.size() == 1, () -> new WebScriptException(Status.STATUS_BAD_REQUEST, "You can only ask one question at a time."));

Question question = questions.get(0);
QuestionModel question = questions.get(0);

log.info("Received question: {}", question);

return List.of(question.withId("questionId"));
String questionId = hxInsightClient.askQuestion(question.toQuestion());

return List.of(question.withId(questionId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,31 @@
import lombok.ToString;
import lombok.experimental.Accessors;

import org.alfresco.hxi_connector.hxi_extension.service.model.Question;

@Accessors(prefix = {"_", ""})
@ToString
@Getter
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(NON_NULL)
@SuppressWarnings("PMD.FieldNamingConventions")
public class Question
public class QuestionModel
{
private String _questionId;
@NotBlank
private String question;
@NotBlank
private String restrictionQuery;

public Question withId(String questionId)
public QuestionModel withId(String questionId)
{
this._questionId = questionId;
return this;
}

public Question toQuestion()
{
return new Question(question, restrictionQuery);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* #%L
* Alfresco HX Insight Connector
* %%
* Copyright (C) 2023 - 2024 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/

package org.alfresco.hxi_connector.hxi_extension.service;

import static org.apache.hc.core5.http.ContentType.APPLICATION_JSON;
import static org.apache.hc.core5.http.HttpStatus.SC_ACCEPTED;

import static org.alfresco.hxi_connector.common.util.ErrorUtils.throwExceptionOnUnexpectedStatusCode;

import java.io.IOException;
import java.util.Map;
import jakarta.annotation.PreDestroy;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Cleanup;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.StringEntity;

import org.alfresco.hxi_connector.common.exception.HxInsightConnectorRuntimeException;
import org.alfresco.hxi_connector.hxi_extension.service.config.QuestionServiceConfig;
import org.alfresco.hxi_connector.hxi_extension.service.model.Question;
import org.alfresco.hxi_connector.hxi_extension.service.util.AuthService;

@Slf4j
@RequiredArgsConstructor
public class HxInsightClient
{
private final static String QUESTION_ID_ENTRY = "questionId";
private final QuestionServiceConfig config;
private final AuthService authService;
private final ObjectMapper objectMapper;
private final CloseableHttpClient client = HttpClients.createDefault();

public String askQuestion(Question question)
{
try
{
@Cleanup
HttpEntity body = new StringEntity(objectMapper.writeValueAsString(question), APPLICATION_JSON);

HttpPost httpPost = new HttpPost(config.getQuestionUrl());
httpPost.setEntity(body);

authService.setAuthHeader(httpPost);

return client.execute(httpPost, (response) -> {
throwExceptionOnUnexpectedStatusCode(response.getCode(), SC_ACCEPTED);

return objectMapper.readValue(response.getEntity().getContent(), new TypeReference<Map<String, String>>() {}).get(QUESTION_ID_ENTRY);
});
}
catch (IOException e)
{
throw new HxInsightConnectorRuntimeException("Failed to ask question", e);
}
}

@PreDestroy
public void close()
{
try
{
log.trace("Closing the HTTP client");
client.close();
}
catch (IOException e)
{
log.error("Failed to close the HTTP client", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* #%L
* Alfresco HX Insight Connector
* %%
* Copyright (C) 2023 - 2024 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/

package org.alfresco.hxi_connector.hxi_extension.service.config;

import jakarta.validation.constraints.NotBlank;

import lombok.Getter;

@Getter

public final class QuestionServiceConfig
{
private final String questionUrl;

public QuestionServiceConfig(@NotBlank String baseUrl)
{
this.questionUrl = baseUrl + "/v1/questions";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* #%L
* Alfresco HX Insight Connector
* %%
* Copyright (C) 2023 - 2024 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/

package org.alfresco.hxi_connector.hxi_extension.service.model;

import jakarta.validation.constraints.NotBlank;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class Question
{
@NotBlank
private final String question;
@NotBlank
private final String restrictionQuery;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* #%L
* Alfresco HX Insight Connector
* %%
* Copyright (C) 2023 - 2024 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/

package org.alfresco.hxi_connector.hxi_extension.service.util;

import lombok.RequiredArgsConstructor;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.core5.http.HttpHeaders;

import org.alfresco.hxi_connector.common.adapters.auth.AccessTokenProvider;

@RequiredArgsConstructor
public class AuthService
{
private final AccessTokenProvider accessTokenProvider;

public void setAuthHeader(HttpPost request)
{
request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessTokenProvider.getAccessToken("hyland-experience"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ hxi.auth.providers.hyland-experience.environment-key=hxi-env-key
hxi.auth.retry.attempts=3
hxi.auth.retry.initial-delay=500
hxi.auth.retry.delay-multiplier=2

hxi.client.baseUrl=http://localhost:8001
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<property name="predictionService" ref="PredictionService"/>
</bean>

<bean class="org.alfresco.hxi_connector.hxi_extension.service.config.QuestionServiceConfig" id="questionServiceConfig">
<constructor-arg name="baseUrl" value="${hxi.client.baseUrl}"/>
</bean>

<bean id="hxi.simpleMappingExceptionResolver" abstract="true" parent="simpleMappingExceptionResolverParent">
<property name="exceptionMappings">
<map merge="true">
Expand Down Expand Up @@ -129,5 +133,17 @@
<constructor-arg name="authenticationClient" ref="hxInsightAuthClient"/>
</bean>

<bean class="org.alfresco.hxi_connector.hxi_extension.rest.api.QuestionsEntityResource"/>
<bean id="authService" class="org.alfresco.hxi_connector.hxi_extension.service.util.AuthService">
<constructor-arg name="accessTokenProvider" ref="hxiAccessTokenProvider"/>
</bean>

<bean class="org.alfresco.hxi_connector.hxi_extension.service.HxInsightClient" id="hxInsightClient">
<constructor-arg name="config" ref="questionServiceConfig"/>
<constructor-arg name="authService" ref="authService"/>
<constructor-arg name="objectMapper" ref="alfrescoEventObjectMapper"/>
</bean>

<bean class="org.alfresco.hxi_connector.hxi_extension.rest.api.QuestionsEntityResource">
<constructor-arg name="hxInsightClient" ref="hxInsightClient"/>
</bean>
</beans>
Loading

0 comments on commit 605df12

Please sign in to comment.