1 | package org.atea.nlptools.koreromaoriinterface;
|
---|
2 |
|
---|
3 | import java.io.FileInputStream;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.io.PrintWriter;
|
---|
6 | import java.lang.reflect.Type;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Properties;
|
---|
10 |
|
---|
11 | import javax.servlet.ServletException;
|
---|
12 | import javax.servlet.annotation.MultipartConfig;
|
---|
13 | import javax.servlet.http.HttpServlet;
|
---|
14 | import javax.servlet.http.HttpServletRequest;
|
---|
15 | import javax.servlet.http.HttpServletResponse;
|
---|
16 | import javax.servlet.http.Part;
|
---|
17 |
|
---|
18 | import com.google.gson.FieldNamingPolicy;
|
---|
19 | import com.google.gson.Gson;
|
---|
20 | import com.google.gson.GsonBuilder;
|
---|
21 | import com.google.gson.reflect.TypeToken;
|
---|
22 |
|
---|
23 | import org.apache.logging.log4j.LogManager;
|
---|
24 | import org.apache.logging.log4j.Logger;
|
---|
25 | import org.atea.nlptools.koreromaoriinterface.exceptions.ReoTuhituhiException;
|
---|
26 | import org.atea.nlptools.koreromaoriinterface.models.AudioFilePart;
|
---|
27 | import org.atea.nlptools.koreromaoriinterface.models.MyTranscriptionResponse;
|
---|
28 | import org.atea.nlptools.koreromaoriinterface.models.TranscriptionResult;
|
---|
29 | import org.atea.nlptools.koreromaoriinterface.services.ReoTuhituhiApiService;
|
---|
30 |
|
---|
31 | @MultipartConfig
|
---|
32 | public class TranscriptionServlet extends HttpServlet
|
---|
33 | {
|
---|
34 | private static final long serialVersionUID = 1L;
|
---|
35 |
|
---|
36 | private static final Logger logger = LogManager.getLogger(TranscriptionServlet.class);
|
---|
37 | private static final Type transcriptionListType = new TypeToken<List<TranscriptionResult>>(){}.getType();
|
---|
38 |
|
---|
39 | private final Gson jsonSerialiser;
|
---|
40 |
|
---|
41 | private ReoTuhituhiApiService transcriptionService;
|
---|
42 |
|
---|
43 | public TranscriptionServlet()
|
---|
44 | {
|
---|
45 | jsonSerialiser = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public void init()
|
---|
50 | {
|
---|
51 | Properties prop = new Properties();
|
---|
52 |
|
---|
53 | try {
|
---|
54 | FileInputStream fis = new FileInputStream("../webapps/gs3-koreromaori/config.properties");
|
---|
55 | prop.load(fis);
|
---|
56 | fis.close();
|
---|
57 | } catch (IOException e) {
|
---|
58 | e.printStackTrace();
|
---|
59 | }
|
---|
60 |
|
---|
61 | String apiEndpoint = prop.getProperty("tuhituhi.api.endpoint");
|
---|
62 | String apiKey = prop.getProperty("tuhituhi.api.key");
|
---|
63 |
|
---|
64 | this.transcriptionService = new ReoTuhituhiApiService(jsonSerialiser, apiEndpoint, apiKey);
|
---|
65 |
|
---|
66 | logger.info("Initialised!");
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
---|
71 | throws IOException
|
---|
72 | {
|
---|
73 | response.sendError(403);
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
---|
78 | throws ServletException, IOException
|
---|
79 | {
|
---|
80 | logger.trace("POST request received.");
|
---|
81 |
|
---|
82 | response.setContentType("application/json; charset=UTF-8");
|
---|
83 | PrintWriter writer = response.getWriter();
|
---|
84 |
|
---|
85 | String audioFileKeysParameter = request.getParameter("audioFileKeys");
|
---|
86 | if (audioFileKeysParameter == null) {
|
---|
87 | response.sendError(400, "Form data was incorrect: missing audioFileKeys part.");
|
---|
88 | return;
|
---|
89 | }
|
---|
90 | String[] audioFileKeys = audioFileKeysParameter.split("\\|");
|
---|
91 |
|
---|
92 | // Create our own representation of each part
|
---|
93 | ArrayList<AudioFilePart> audioFileParts = new ArrayList<AudioFilePart>(audioFileKeys.length);
|
---|
94 | for (String audioFileKey : audioFileKeys)
|
---|
95 | {
|
---|
96 | Part p = request.getPart(audioFileKey);
|
---|
97 | AudioFilePart audioFilePart = AudioFilePart.fromPart(p);
|
---|
98 | audioFileParts.add(audioFilePart);
|
---|
99 | }
|
---|
100 |
|
---|
101 | try
|
---|
102 | {
|
---|
103 | List<AudioFilePart> results = transcriptionService.getTranscriptions(audioFileParts);
|
---|
104 |
|
---|
105 | List<MyTranscriptionResponse> responses = new ArrayList<MyTranscriptionResponse>(results.size());
|
---|
106 | for (AudioFilePart result : results) {
|
---|
107 | responses.add(MyTranscriptionResponse.FromTranscriptionResult(result.getTranscriptionResult(), result.fileName));
|
---|
108 | }
|
---|
109 |
|
---|
110 | String json = jsonSerialiser.toJson(responses, transcriptionListType);
|
---|
111 | writer.append(json);
|
---|
112 | }
|
---|
113 | catch (ReoTuhituhiException rtex)
|
---|
114 | {
|
---|
115 | response.sendError(502, "Call to the Reo Tuhituhi API failed.");
|
---|
116 | }
|
---|
117 | catch (Exception ex)
|
---|
118 | {
|
---|
119 | response.sendError(500, "Failed to process the request.");
|
---|
120 | logger.error("Failed to complete API call", ex);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|