2018-02-20 08:39:31 +01:00
|
|
|
package org.gcube.nlphub;
|
|
|
|
|
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.annotation.MultipartConfig;
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.servlet.http.Part;
|
|
|
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import org.gcube.nlphub.legacy.Constants;
|
|
|
|
import org.gcube.nlphub.legacy.JsonManager;
|
|
|
|
import org.gcube.nlphub.legacy.NlpHubException;
|
2018-02-22 17:48:15 +01:00
|
|
|
import org.gcube.nlphub.workspace.WorkspaceManager;
|
2018-02-20 08:39:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Servlet implementation class NLPUploader
|
|
|
|
*/
|
|
|
|
// @WebServlet("/NLPUploader")
|
|
|
|
@WebServlet(asyncSupported = true, name = "NLPUploader", urlPatterns = { "/nlphub-uploader-servlet" })
|
|
|
|
@MultipartConfig
|
|
|
|
public class NLPUploader extends HttpServlet {
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private Logger logger = Logger.getLogger(NLPUploader.class.getSimpleName());
|
|
|
|
private boolean devMode = true;
|
|
|
|
private String token = "df2cc5f5-63ee-48c1-b2a6-1210030c57b8-843339462";
|
2018-02-22 17:48:15 +01:00
|
|
|
private WorkspaceManager ws;
|
|
|
|
|
2018-02-20 08:39:31 +01:00
|
|
|
/**
|
|
|
|
* @see HttpServlet#HttpServlet()
|
|
|
|
*/
|
|
|
|
public NLPUploader() {
|
|
|
|
super();
|
|
|
|
// TODO Auto-generated constructor stub
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
|
|
|
|
* response)
|
|
|
|
*/
|
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
throws ServletException, IOException {
|
|
|
|
doWork(request, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
|
|
|
|
* response)
|
|
|
|
*/
|
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
throws ServletException, IOException {
|
2018-02-22 17:48:15 +01:00
|
|
|
token = Constants.getToken(request, devMode);
|
2018-02-20 08:39:31 +01:00
|
|
|
doWork(request, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
2018-02-22 17:48:15 +01:00
|
|
|
ws = new WorkspaceManager();
|
2018-02-20 08:39:31 +01:00
|
|
|
response.setContentType("application/json;charset=UTF-8");
|
|
|
|
if (request.getParameter("freetext") == null)
|
|
|
|
handleFileUpload(request, response);
|
|
|
|
else
|
|
|
|
handleFreeText(request, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleFreeText(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
throws ServletException, IOException {
|
|
|
|
String freeText = request.getParameter("freetext");
|
|
|
|
System.out.println(freeText);
|
|
|
|
byte[] content = freeText.getBytes("UTF-8");
|
|
|
|
String fileName = generateFileName();
|
|
|
|
PrintWriter writer = response.getWriter();
|
|
|
|
try {
|
2018-02-22 17:48:15 +01:00
|
|
|
if (!ws.uploadFile(content, fileName, Constants.DEFAULT_DESCRIPTION, token)) {
|
2018-02-20 08:39:31 +01:00
|
|
|
writer.println(new JsonManager().getErrorJsonResponse(
|
|
|
|
"Error uploading file. A file called '" + fileName + "' is already in the workspace?"));
|
|
|
|
return;
|
|
|
|
}
|
2018-02-22 17:48:15 +01:00
|
|
|
String link = ws.getPublicLink(fileName, token);
|
2018-02-20 08:39:31 +01:00
|
|
|
writer.println(new JsonManager().getSuccessJsonResponse("" + link));
|
|
|
|
} catch (Exception x) {
|
|
|
|
x.printStackTrace();
|
|
|
|
logger.error(x.getClass().getName() + ": " + x.getLocalizedMessage());
|
|
|
|
writer.println(new JsonManager().getErrorJsonResponse(x.getLocalizedMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleFileUpload(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
throws ServletException, IOException {
|
|
|
|
int contentLength = request.getContentLength();
|
|
|
|
|
|
|
|
Part filePart = request.getPart("mytxtfile");
|
|
|
|
|
|
|
|
String fileName = getFileName(filePart);
|
|
|
|
|
|
|
|
PrintWriter writer = response.getWriter();
|
|
|
|
// allocate a buffer of request size
|
|
|
|
byte[] buffer = new byte[contentLength];
|
|
|
|
byte[] bufferedContent;
|
|
|
|
try {
|
|
|
|
InputStream fileContent = filePart.getInputStream();
|
|
|
|
int offset = 0, len = 100, byteRead = 0;
|
|
|
|
byte[] readBuffer = new byte[len];
|
|
|
|
while (byteRead > -1) {
|
|
|
|
byteRead = fileContent.read(readBuffer, 0, len);
|
|
|
|
// System.out.println(byteRead);
|
|
|
|
if (byteRead > 0) {
|
|
|
|
System.arraycopy(readBuffer, 0, buffer, offset, byteRead);
|
|
|
|
offset += byteRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset < contentLength) {
|
|
|
|
bufferedContent = new byte[offset];
|
|
|
|
System.arraycopy(buffer, 0, bufferedContent, 0, offset);
|
|
|
|
} else
|
|
|
|
bufferedContent = buffer;
|
|
|
|
|
2018-02-22 17:48:15 +01:00
|
|
|
ws.deleteFile(fileName, token);
|
2018-02-20 08:39:31 +01:00
|
|
|
|
2018-02-22 17:48:15 +01:00
|
|
|
if (!ws.uploadFile(bufferedContent, fileName, Constants.DEFAULT_DESCRIPTION, token)) {
|
2018-02-20 08:39:31 +01:00
|
|
|
writer.println(new JsonManager().getErrorJsonResponse(
|
|
|
|
"Error uploading file. A file called '" + fileName + "' is already in the workspace?"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-22 17:48:15 +01:00
|
|
|
String link = ws.getPublicLink(fileName, token);
|
2018-02-20 08:39:31 +01:00
|
|
|
writer.println(new JsonManager().getSuccessJsonResponse("" + link));
|
|
|
|
} catch (Exception x) {
|
|
|
|
x.printStackTrace();
|
|
|
|
logger.error(x.getClass().getName() + ": " + x.getLocalizedMessage());
|
|
|
|
writer.println(new JsonManager().getErrorJsonResponse(x.getLocalizedMessage()));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getFileName(Part part) {
|
|
|
|
String partHeader = part.getHeader("content-disposition");
|
|
|
|
logger.debug("Part Header: " + partHeader);
|
|
|
|
for (String content : part.getHeader("content-disposition").split(";")) {
|
|
|
|
if (content.trim().startsWith("filename")) {
|
|
|
|
return content.substring(content.indexOf('=') + 1).trim().replace("\"", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-22 17:48:15 +01:00
|
|
|
// private String getPublicLink(String fileName, String token) throws NlpHubException {
|
|
|
|
// try {
|
|
|
|
// String link = "";
|
|
|
|
// String user = authorizationService().get(token).getClientInfo().getId();
|
|
|
|
// String wsRoot = "/Home/" + user + "/Workspace/";
|
|
|
|
// String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
|
|
|
|
// String uri = webapp + "/rest/GetPublicLink?absPath=" + URLEncoder.encode(wsRoot + fileName, "UTF-8")
|
|
|
|
// + "&shortUrl=false";
|
|
|
|
// URL url = new URL(uri);
|
|
|
|
//
|
|
|
|
// // System.out.println(uri);
|
|
|
|
//
|
|
|
|
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
// connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
|
|
|
|
// connection.setDoInput(true);
|
|
|
|
// connection.setDoOutput(true);
|
|
|
|
// connection.setUseCaches(false);
|
|
|
|
// connection.setRequestMethod("GET");
|
|
|
|
//
|
|
|
|
// BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
//
|
|
|
|
// StringBuffer response = new StringBuffer();
|
|
|
|
// String inputLine;
|
|
|
|
// while ((inputLine = r.readLine()) != null) {
|
|
|
|
// response.append(inputLine);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// String xmlOut = response.toString();
|
|
|
|
// // System.out.println("xmlOut: " + xmlOut);
|
|
|
|
//
|
|
|
|
// String begin = "<string>";
|
|
|
|
// String end = "</string>";
|
|
|
|
// int b = xmlOut.indexOf(begin);
|
|
|
|
// int e = xmlOut.indexOf(end);
|
|
|
|
//
|
|
|
|
// if (xmlOut.contains("Exception") || (e < 0) || (b < 0)) {
|
|
|
|
// String message = "Invalid link: " + URLEncoder.encode(xmlOut, "UTF-8");
|
|
|
|
// logger.error(message);
|
|
|
|
// throw new NlpHubException(message, null);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// link = xmlOut.substring(b + begin.length(), e);
|
|
|
|
// return link;
|
|
|
|
//
|
|
|
|
// } catch (Exception e) {
|
|
|
|
// logger.error(e.getLocalizedMessage());
|
|
|
|
// throw new NlpHubException(e.getLocalizedMessage(), e);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// private void deleteFile(String fileName, String token) throws NlpHubException {
|
|
|
|
// try {
|
|
|
|
// String user = authorizationService().get(token).getClientInfo().getId();
|
|
|
|
// String wsRoot = "/Home/" + user + "/Workspace/";
|
|
|
|
// String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
|
|
|
|
// String uri = webapp + "/rest/Delete?absPath=" + URLEncoder.encode(wsRoot + fileName, "UTF-8");
|
|
|
|
// URL url = new URL(uri);
|
|
|
|
//
|
|
|
|
// // System.out.println(uri);
|
|
|
|
//
|
|
|
|
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
// connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
|
|
|
|
// connection.setDoInput(true);
|
|
|
|
// connection.setDoOutput(true);
|
|
|
|
// connection.setUseCaches(false);
|
|
|
|
// connection.setRequestMethod("GET");
|
|
|
|
//
|
|
|
|
// BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
//
|
|
|
|
// StringBuffer response = new StringBuffer();
|
|
|
|
// String inputLine;
|
|
|
|
// while ((inputLine = r.readLine()) != null) {
|
|
|
|
// response.append(inputLine);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// String xmlOut = response.toString();
|
|
|
|
// // System.out.println(xmlOut);
|
|
|
|
// } catch (Exception e) {
|
|
|
|
// logger.error(e.getLocalizedMessage());
|
|
|
|
// throw new NlpHubException(e.getLocalizedMessage(), e);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// private boolean uploadFile(byte[] in, String name, String description, String token) throws NlpHubException {
|
|
|
|
// OutputStream output = null;
|
|
|
|
// try {
|
|
|
|
// String user = authorizationService().get(token).getClientInfo().getId();
|
|
|
|
// String wsRoot = "/Home/" + user + "/Workspace/";
|
|
|
|
// String webapp = "https://workspace-repository.d4science.org/home-library-webapp";
|
|
|
|
// String uri = webapp + "/rest/Upload?name=" + URLEncoder.encode(name, "UTF-8") + "&description="
|
|
|
|
// + URLEncoder.encode(description, "UTF-8") + "&parentPath=" + URLEncoder.encode(wsRoot, "UTF-8");
|
|
|
|
// URL url = new URL(uri);
|
|
|
|
//
|
|
|
|
// // System.out.println(uri);
|
|
|
|
//
|
|
|
|
// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
// connection.setRequestProperty(Constants.TOKEN_PARAMETER, token);
|
|
|
|
// connection.setDoInput(true);
|
|
|
|
// connection.setDoOutput(true);
|
|
|
|
// connection.setUseCaches(false);
|
|
|
|
// connection.setRequestProperty(Constants.CONTENT_TYPE, Constants.MIME_TEXT);
|
|
|
|
// connection.setRequestMethod("POST");
|
|
|
|
// output = connection.getOutputStream();
|
|
|
|
// output.write(in);
|
|
|
|
//
|
|
|
|
// BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
//
|
|
|
|
// StringBuffer response = new StringBuffer();
|
|
|
|
// String inputLine;
|
|
|
|
// while ((inputLine = r.readLine()) != null) {
|
|
|
|
// response.append(inputLine);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// String xmlOut = response.toString();
|
|
|
|
// // System.out.println(xmlOut);
|
|
|
|
// if (xmlOut.contains("Exception"))
|
|
|
|
// return false;
|
|
|
|
// return true;
|
|
|
|
//
|
|
|
|
// } catch (Exception e) {
|
|
|
|
// logger.error(e.getLocalizedMessage());
|
|
|
|
// throw new NlpHubException(e.getLocalizedMessage(), e);
|
|
|
|
// } finally {
|
|
|
|
// // output stream must be closed anyway...
|
|
|
|
// if (output != null)
|
|
|
|
// try {
|
|
|
|
// output.close();
|
|
|
|
// } catch (IOException e) {
|
|
|
|
// logger.error(e.getLocalizedMessage());
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2018-02-20 08:39:31 +01:00
|
|
|
|
|
|
|
private String generateFileName() {
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
return "auto-nlp-" + now;
|
|
|
|
}
|
|
|
|
}
|