1 | package org.atea.nlptools.koreromaoriinterface;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.InputStream;
|
---|
5 | import java.io.PrintWriter;
|
---|
6 | import java.lang.reflect.Type;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import javax.servlet.ServletException;
|
---|
11 | import javax.servlet.annotation.MultipartConfig;
|
---|
12 | import javax.servlet.http.HttpServlet;
|
---|
13 | import javax.servlet.http.HttpServletRequest;
|
---|
14 | import javax.servlet.http.HttpServletResponse;
|
---|
15 |
|
---|
16 | import com.google.gson.FieldNamingPolicy;
|
---|
17 | import com.google.gson.Gson;
|
---|
18 | import com.google.gson.GsonBuilder;
|
---|
19 | import com.google.gson.reflect.TypeToken;
|
---|
20 |
|
---|
21 | import org.apache.logging.log4j.LogManager;
|
---|
22 | import org.apache.logging.log4j.Logger;
|
---|
23 | import org.atea.nlptools.koreromaoriinterface.models.MyTranscriptionResponse;
|
---|
24 | import org.atea.nlptools.koreromaoriinterface.models.TranscriptionResult;
|
---|
25 | import org.atea.nlptools.koreromaoriinterface.services.ReoTuhituhiApiService;
|
---|
26 |
|
---|
27 | @MultipartConfig
|
---|
28 | public class TranscriptionServlet extends HttpServlet
|
---|
29 | {
|
---|
30 | private static final long serialVersionUID = 1L;
|
---|
31 |
|
---|
32 | private static final Logger logger = LogManager.getLogger(TranscriptionServlet.class);
|
---|
33 | private static final Type transcriptionListType = new TypeToken<List<TranscriptionResult>>(){}.getType();
|
---|
34 |
|
---|
35 | private final Gson jsonSerialiser;
|
---|
36 | private final ReoTuhituhiApiService transcriptionService;
|
---|
37 |
|
---|
38 | public TranscriptionServlet()
|
---|
39 | {
|
---|
40 | jsonSerialiser = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
|
---|
41 | this.transcriptionService = new ReoTuhituhiApiService(jsonSerialiser);
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
---|
46 | throws IOException
|
---|
47 | {
|
---|
48 | response.sendError(403);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
---|
53 | throws ServletException, IOException
|
---|
54 | {
|
---|
55 | logger.trace("POST request received.");
|
---|
56 |
|
---|
57 | response.setContentType("application/json; charset=UTF-8");
|
---|
58 | PrintWriter writer = response.getWriter();
|
---|
59 |
|
---|
60 | String audioFileKeysParameter = request.getParameter("audioFileKeys");
|
---|
61 | if (audioFileKeysParameter == null) {
|
---|
62 | response.sendError(400, "Form data was incorrect: missing audioFileKeys part.");
|
---|
63 | return;
|
---|
64 | }
|
---|
65 | String[] audioFileKeys = audioFileKeysParameter.split("\\|");
|
---|
66 |
|
---|
67 | ArrayList<InputStream> audioFileStreams = new ArrayList<InputStream>(audioFileKeys.length);
|
---|
68 | for (String audioFileKey : audioFileKeys)
|
---|
69 | {
|
---|
70 | audioFileStreams.add(request.getPart(audioFileKey).getInputStream());
|
---|
71 | }
|
---|
72 |
|
---|
73 | try
|
---|
74 | {
|
---|
75 | List<TranscriptionResult> results = transcriptionService.GetTranscriptions(audioFileStreams);
|
---|
76 | List<MyTranscriptionResponse> responses = new ArrayList<MyTranscriptionResponse>(results.size());
|
---|
77 |
|
---|
78 | for (TranscriptionResult result : results) {
|
---|
79 | responses.add(MyTranscriptionResponse.FromTranscriptionResult(result, "placeholder")); // TODO: Insert correct file name
|
---|
80 | }
|
---|
81 |
|
---|
82 | String json = jsonSerialiser.toJson(responses, transcriptionListType);
|
---|
83 | writer.append(json);
|
---|
84 | }
|
---|
85 | catch (Exception ex)
|
---|
86 | {
|
---|
87 | response.sendError(500, "Call to the Reo Tuhituhi API failed." + ex);
|
---|
88 | logger.error("Failed to complete API call", ex);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|