Skip to content
Snippets Groups Projects
Commit 4dd70103 authored by Lange, Dr. Herbert's avatar Lange, Dr. Herbert
Browse files

add additional handlers to have a working local corpus services interface

parent 9e089401
Branches
No related tags found
No related merge requests found
package de.uni_hamburg.corpora.server;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Favicon
*/
@Path("/{fileName: .*ico}")
public class Favicon {
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces({"image/x-icon"})
public Response getFavicon() {
logger.info("Here");
return Response.ok().build() ;
}
}
package de.uni_hamburg.corpora.server;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
/**
* @author bba1792 Dr. Herbert Lange
* @version 20211004
* Resource to show an existing local report
* Scope: local
*/
@Path("report")
public class Report {
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
/**
* Method handling HTTP GET requests for the local report.
*
* @return Response containing the file or an error code
*/
@GET
public Response getReport() {
String reportFileName = System.getProperty("java.io.tmpdir") + "/corpus-files/report.html";
logger.info("Loading report file " + reportFileName);
File reportFile = new File(reportFileName);
if (!reportFile.exists()) {
logger.info("Report is missing");
return Response.status(404,"Invalid report file").build();
}
else {
try {
return Response.ok(new FileInputStream(reportFile), Files.probeContentType(reportFile.toPath())).build();
} catch (IOException e) {
return Response.status(500, "Error loading file").build();
}
}
}
}
package de.uni_hamburg.corpora.server;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.util.Base64;
/**
* @author bba1792 Dr. Herbert Lange
* @version 20211004
* Class handling files sent to the server
*/
@Path("/send")
public class SendFile {
public static class CorpusFile {
String name;
String type;
byte[] data;
@JsonCreator
public CorpusFile(
@JsonProperty("name") String name,
@JsonProperty("data") String data) throws IOException {
this.name = name;
String[] parts = data.split(";");
if (parts.length == 1)
throw new IOException("Invalid data") ;
this.type = parts[0].substring(5);
this.data = Base64.getDecoder().decode(parts[1].substring(7));
}
public String toString() {
return "name:" + this.name + ", type:" + this.type + ", data:" + new String(this.data);
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public byte[] getData() {
return data;
}
}
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
/**
* Accepts files sent via post and data-url-encoded. Only for local use because we don't avoid potential
* conflicts with parallel uploads!
*
* @return Response containing the file or an error code
*/
@POST
public Response getFile(String fileData) {
// New folder in temp
String corpusDirStr = System.getProperty("java.io.tmpdir") + "/corpus-files";
File corpusDir = new File(corpusDirStr);
// Create if folder is missing
if (!corpusDir.exists())
corpusDir.mkdirs();
try {
// Parse json
CorpusFile cf = new ObjectMapper().readerFor(CorpusFile.class).readValue(fileData);
//logger.info("Got: " + cf);
// Create a new file in temp folder and write the data we got
File file = new File(new URI(corpusDir.toURI() + "/" + cf.getName())) ;
// Delete file if it already exists
if (file.exists())
file.delete();
if (!file.createNewFile()) throw new IOException("Cannot create file");
logger.info(String.valueOf(file.toURI()));
FileOutputStream fs = new FileOutputStream(file);
fs.write(cf.getData());
fs.close();
// Everything okay
return Response.ok().build();
} catch (IOException | URISyntaxException e) {
// On exception print error and return error code
logger.error("Error reading " + fileData + ": " + e);
e.printStackTrace();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),e.toString()).build();
}
}
}
package de.uni_hamburg.corpora.server;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
/**
* @author bba1792 Dr. Herbert Lange
* @version 20211004
* Class handling static files
*/
@Path("/static/{staticFile}")
public class Static {
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
/**
* Method handling HTTP GET requests for static files. The returned object will be sent
* to the client
*
* @return Response containing the file or an error code
*/
@GET
public Response getStatic(@PathParam("staticFile") String fileName) {
logger.info("Loading file " + fileName);
// Get file from resource
URL resource = this.getClass().getClassLoader().getResource("static/"+ fileName);
if (resource == null) {
logger.info("Resource is null");
return Response.status(404,"Invalid static file").build();
}
else {
try {
File staticFile = new File(resource.toURI());
logger.info("Resource is " + resource.toURI());
return Response.ok(new FileInputStream(staticFile),Files.probeContentType(staticFile.toPath())).build();
} catch (URISyntaxException | IOException e) {
return Response.status(500, "Error loading file").build();
}
}
}
}
package de.uni_hamburg.corpora.server;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.Template;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.StringWriter;
import java.net.URL;
/**
* /**
* @author bba1792 Dr. Herbert Lange
* @version 20211004
* Root resource (exposed at "/" path)
*/
@Path("/")
public class WebRoot {
private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces(MediaType.TEXT_HTML)
public Response getRoot() {
ClassLoader cl = this.getClass().getClassLoader();
URL res = this.getClass().getClassLoader().getResource("templates/root.vm");
if (res == null)
return Response.status(500,"Template not found").build();
StringWriter result = new StringWriter();
VelocityContext context = new VelocityContext();
context.put("functions", new ListCorpusFunctions().listFunctions());
Velocity.mergeTemplate("templates/root.vm", "UTF-8",context,result) ;
return Response.ok(result.toString()).build() ;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment