Ignore:
Timestamp:
2021-11-08T15:33:27+13:00 (2 years ago)
Author:
cstephen
Message:

Implement json response for file macronisation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • other-projects/the-macronizer/trunk/src/java/web/servlets/FileUpload.java

    r35720 r35721  
    1313import javax.servlet.http.HttpServletResponse;
    1414
     15import com.google.gson.Gson;
     16import com.google.gson.stream.JsonWriter;
     17
    1518import monogram.plugin.PluginConfiguration;
    1619import monogram.plugin.PluginManager;
     
    2528
    2629/**
     30 * Represents a servlet for macronising the contents of a file.
    2731 * @author University of Waikato - Te Whare Wānanga o Waikato
    28  * @version 1.0
    29  * @since   2014-11-20
     32 * @version 2.0
     33 * @since 2014-11-20
    3034 */
    3135public class FileUpload extends HttpServlet {
     
    3337    // Create an instance of the logger object defined for the DirectInput servlet in log4j.properties.
    3438    private static final Logger logger = Logger.getLogger(web.servlets.DirectInput.class.getName());
     39
     40    private final Gson gsonInstance = new Gson();
    3541
    3642    private File tmpdir;
     
    115121
    116122            restoredFile = pluginManager.run(configuration);
    117             request.setAttribute("file", restoredFile);
    118             request.setAttribute("fileType", configuration.getFileType());
    119             request.setAttribute("charsetEncoding", "utf-8");
    120             request.setAttribute("filename", properties.getFilename());
    121             request.setAttribute("preserveMacrons", properties.getPreserveExistingMacrons());
    122             request.setAttribute("options", properties.getShowAdvancedOptions());
    123 
    124             forward(properties.getJspForwardPath() + "/main.jsp", request, response);
     123
     124            if (properties.getOutputType() == OutputType.Json)
     125            {
     126                response.setContentType("application/json; charset=UTF-8");
     127                JsonWriter writer = gsonInstance.newJsonWriter(response.getWriter());
     128                writer.beginObject();
     129
     130                writer.name("fileName");
     131                writer.value(properties.getFileName());
     132
     133                writer.name("filePath");
     134                writer.value(restoredFile.getPath());
     135
     136                writer.name("fileType");
     137                writer.value(configuration.getFileType());
     138               
     139                writer.endObject();
     140                writer.flush();
     141            }
     142            else
     143            {
     144                request.setAttribute("file", restoredFile);
     145                request.setAttribute("fileType", configuration.getFileType());
     146                request.setAttribute("charsetEncoding", "utf-8");
     147                request.setAttribute("filename", properties.getFileName());
     148                request.setAttribute("preserveMacrons", properties.getPreserveExistingMacrons());
     149                request.setAttribute("options", properties.getShowAdvancedOptions());
     150
     151                forward(properties.getJspForwardPath() + "/main.jsp", request, response);
     152            }
    125153        }
    126154        catch (UnsupportedOperationException uoex)
     
    305333    {
    306334        final File file = properties.getFile();
    307         final String fileType = properties.getFileType().equals("(detect automatically)") ? FileUtil.guessFileType(file) : properties.getFileType();
    308         final String charsetEncoding = properties.getCharsetEncoding().equals("(detect automatically)") ? "utf8" : properties.getCharsetEncoding();
     335
     336        String fileType = properties.getFileType();
     337        if (fileType == null || fileType.equals("(detect automatically)"))
     338        {
     339            fileType = FileUtil.guessFileType(file);
     340        }
     341
     342        String charsetEncoding = properties.getCharsetEncoding();
     343        if (charsetEncoding == null || charsetEncoding.equals("(detect automatically)"))
     344        {
     345            charsetEncoding = "utf8";
     346        }
    309347
    310348        final PluginConfiguration configuration = new PluginConfiguration();
     
    328366        private OutputType outputType;
    329367
     368        public Properties() {
     369            this.outputType = OutputType.JspRedirect;
     370        }
     371
    330372        public File getFile() {
    331373            return file;
    332374        }
    333375
    334         public String getFilename() {
     376        public String getFileName() {
    335377            return filename;
    336378        }
Note: See TracChangeset for help on using the changeset viewer.