1 | package org.atea.nlptools.koreromaoriproxy;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.PrintWriter;
|
---|
5 | import java.lang.reflect.Type;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import javax.servlet.ServletException;
|
---|
10 | import javax.servlet.annotation.MultipartConfig;
|
---|
11 | import javax.servlet.http.HttpServlet;
|
---|
12 | import javax.servlet.http.HttpServletRequest;
|
---|
13 | import javax.servlet.http.HttpServletResponse;
|
---|
14 | import javax.servlet.http.Part;
|
---|
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.koreromaoriproxy.models.AudioFilePart;
|
---|
24 | import org.atea.nlptools.koreromaoriproxy.models.TranscriptionResult;
|
---|
25 | import org.atea.nlptools.koreromaoriproxy.models.TranscriptionResultDto;
|
---|
26 | import org.atea.nlptools.koreromaoriproxy.models.TranscriptionResultMetadata;
|
---|
27 |
|
---|
28 | @MultipartConfig
|
---|
29 | public class TestServlet extends HttpServlet
|
---|
30 | {
|
---|
31 | private static final long serialVersionUID = 1L;
|
---|
32 |
|
---|
33 | private static final Logger logger = LogManager.getLogger(TranscriptionServlet.class);
|
---|
34 | private static final Type transcriptionListType = new TypeToken<List<TranscriptionResult>>(){}.getType();
|
---|
35 |
|
---|
36 | private final Gson jsonSerialiser;
|
---|
37 |
|
---|
38 | public TestServlet()
|
---|
39 | {
|
---|
40 | jsonSerialiser = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public void init()
|
---|
45 | {
|
---|
46 | logger.info("TestServlet Initialised!");
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
---|
51 | throws IOException
|
---|
52 | {
|
---|
53 | response.getWriter().println("You are using the test endpoint.");
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
---|
58 | throws ServletException, IOException
|
---|
59 | {
|
---|
60 | try
|
---|
61 | {
|
---|
62 | logger.trace("POST request received.");
|
---|
63 |
|
---|
64 | response.setContentType("application/json; charset=UTF-8");
|
---|
65 | PrintWriter writer = response.getWriter();
|
---|
66 |
|
---|
67 | // Attempt to get the key of every audio file part
|
---|
68 | String audioFileKeysParameter = request.getParameter("audioFileKeys");
|
---|
69 | if (audioFileKeysParameter == null)
|
---|
70 | {
|
---|
71 | response.sendError(400, "Form data was incorrect: missing audioFileKeys part.");
|
---|
72 | logger.trace("Request rejected due: Form data was missing the audioFileKeys part.");
|
---|
73 | return;
|
---|
74 | }
|
---|
75 | String[] audioFileKeys = audioFileKeysParameter.split("\\|");
|
---|
76 |
|
---|
77 | // Create our own representation of each part
|
---|
78 | List<AudioFilePart> audioFileParts = new ArrayList<AudioFilePart>(audioFileKeys.length);
|
---|
79 | for (String audioFileKey : audioFileKeys)
|
---|
80 | {
|
---|
81 | // Attempt to get the part that this key specifies should be present
|
---|
82 | Part p = request.getPart(audioFileKey);
|
---|
83 | if (p == null) {
|
---|
84 | response.sendError(400, "Form data was incorrect: a part key listed in the audioFileKeys was not found.");
|
---|
85 | logger.trace("Request rejected due: Form data was missing a client-specified part.");
|
---|
86 | return;
|
---|
87 | }
|
---|
88 |
|
---|
89 | AudioFilePart audioFilePart = AudioFilePart.fromPart(p);
|
---|
90 | audioFileParts.add(audioFilePart);
|
---|
91 | }
|
---|
92 |
|
---|
93 | // Transcribe each part
|
---|
94 | for (AudioFilePart p : audioFileParts) {
|
---|
95 | p.setTranscriptionResult(generateMockTranscription());
|
---|
96 | }
|
---|
97 |
|
---|
98 | AudioFilePart failedTranscription = new AudioFilePart("id10t.wav", null);
|
---|
99 | failedTranscription.setTranscriptionResult(new TranscriptionResult("Ruh roh!"));
|
---|
100 | audioFileParts.add(failedTranscription);
|
---|
101 |
|
---|
102 | // Transform the Tuhituhi response into our own format
|
---|
103 | List<TranscriptionResultDto> responses = new ArrayList<TranscriptionResultDto>(audioFileParts.size());
|
---|
104 | for (AudioFilePart result : audioFileParts) {
|
---|
105 | responses.add(TranscriptionResultDto.FromTranscriptionResult(result.getTranscriptionResult(), result.fileName));
|
---|
106 | }
|
---|
107 |
|
---|
108 | // Send our result to the client
|
---|
109 | String json = jsonSerialiser.toJson(responses, transcriptionListType);
|
---|
110 | writer.append(json);
|
---|
111 | }
|
---|
112 | catch (Exception ex)
|
---|
113 | {
|
---|
114 | response.sendError(500, "Failed to process the request.");
|
---|
115 | logger.error("Failed to complete API call", ex);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | private TranscriptionResult generateMockTranscription()
|
---|
120 | {
|
---|
121 | ArrayList<TranscriptionResultMetadata> metadata = new ArrayList<TranscriptionResultMetadata>(7);
|
---|
122 | String transcription = "";
|
---|
123 |
|
---|
124 | for (int i = 0; i < 8; i++)
|
---|
125 | {
|
---|
126 | transcription += "ngÄ tama a rangi | ";
|
---|
127 | metadata.add(new TranscriptionResultMetadata('Å', 0.5, 0.44));
|
---|
128 | metadata.add(new TranscriptionResultMetadata('Ä', 0.5, 0.46));
|
---|
129 | metadata.add(new TranscriptionResultMetadata(' ', 0.5, 0.66));
|
---|
130 | metadata.add(new TranscriptionResultMetadata('t', 0.5, 0.72));
|
---|
131 | metadata.add(new TranscriptionResultMetadata('a', 0.5, 0.74));
|
---|
132 | metadata.add(new TranscriptionResultMetadata('m', 0.5, 0.90));
|
---|
133 | metadata.add(new TranscriptionResultMetadata('a', 0.5, 0.92));
|
---|
134 | metadata.add(new TranscriptionResultMetadata(' ', 0.5, 1.12));
|
---|
135 | metadata.add(new TranscriptionResultMetadata('a', 0.5, 1.16));
|
---|
136 | metadata.add(new TranscriptionResultMetadata(' ', 0.5, 1.38));
|
---|
137 | metadata.add(new TranscriptionResultMetadata('r', 0.5, 1.42));
|
---|
138 | metadata.add(new TranscriptionResultMetadata('a', 0.5, 1.44));
|
---|
139 | metadata.add(new TranscriptionResultMetadata('Å', 0.5, 1.6));
|
---|
140 | metadata.add(new TranscriptionResultMetadata('i', 0.5, 1.62));
|
---|
141 | metadata.add(new TranscriptionResultMetadata(' ', 0.5, 1.62));
|
---|
142 | metadata.add(new TranscriptionResultMetadata('|', 0.5, 1.62));
|
---|
143 | metadata.add(new TranscriptionResultMetadata(' ', 0.5, 1.62));
|
---|
144 | }
|
---|
145 |
|
---|
146 | return new TranscriptionResult("Generated a mock transcription of akl_mi_pk_0001.wav", metadata, "1.0-fake", true, transcription);
|
---|
147 | }
|
---|
148 | }
|
---|