summaryrefslogtreecommitdiffstats
path: root/Dozentenmodulserver/src/main/java/server/ServerHandler.java
diff options
context:
space:
mode:
Diffstat (limited to 'Dozentenmodulserver/src/main/java/server/ServerHandler.java')
-rw-r--r--Dozentenmodulserver/src/main/java/server/ServerHandler.java401
1 files changed, 401 insertions, 0 deletions
diff --git a/Dozentenmodulserver/src/main/java/server/ServerHandler.java b/Dozentenmodulserver/src/main/java/server/ServerHandler.java
new file mode 100644
index 00000000..b6a50952
--- /dev/null
+++ b/Dozentenmodulserver/src/main/java/server/ServerHandler.java
@@ -0,0 +1,401 @@
+package server;
+
+import java.io.File;
+import java.io.IOException;
+import java.math.BigInteger;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.sql.SQLException;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerException;
+import models.Configuration;
+import org.apache.commons.io.FileUtils;
+import org.apache.log4j.Logger;
+import org.apache.thrift.TException;
+import sql.SQL;
+import util.XMLCreator;
+
+
+
+public class ServerHandler implements Server.Iface {
+ static SQL sql = new SQL();
+
+ private static Logger log = Logger.getLogger(ServerHandler.class);
+
+ @Override
+ public User getFtpUser() throws TException {
+ log.info(new Date() + " - returning FTPUser...");
+ User user = new User();
+ user.setUserName(UUID.randomUUID().toString().substring(0, 8));
+ user.setPassword(getEncodedSha1Sum(UUID.randomUUID().toString()
+ .substring(0, 8)));
+ if (Configuration.config.getAbsolute_path().endsWith("/")) {
+ user.setPath(Configuration.config.getAbsolute_path());
+ } else {
+ user.setPath(Configuration.config.getAbsolute_path() + "/");
+ }
+
+ // check if folder temp and folder prod exist
+ if (folderTempExists() == true && folderProdExists() == true) {
+ sql.writeFTPUser(user.getUserName(), user.getPassword());
+ return user;
+ } else {
+ log.info(new Date() + " - returning null user");
+ return null;
+ }
+ }
+
+ public boolean folderTempExists() {
+ // check if folder temp exists, otherwise create it
+ Path path = null;
+ if (Configuration.config.getAbsolute_path().endsWith("/")) {
+ path = Paths.get(Configuration.config.getAbsolute_path() + "temp");
+ } else {
+ path = Paths.get(Configuration.config.getAbsolute_path() + "/temp");
+ }
+
+ if (Files.exists(path) == true) {
+ log.info(new Date() + " - folder '" + path
+ + "' exists, no further action");
+ return true;
+ } else {
+ // create directory and set permissions
+ boolean success = (new File(path + "")).mkdirs();
+
+ if (!success) {
+ log.info(new Date() + " - failed to create folder '" + path
+ + "'");
+ return false;
+ } else {
+ // set permissions
+ try {
+ Runtime.getRuntime().exec("chmod 777 " + path);
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ log.info(new Date() + " - folder '" + path
+ + "' successfully created");
+ return true;
+ }
+ }
+
+ }// end folderTempExists()
+
+ public boolean folderProdExists() {
+ // check if folder temp exists, otherwise create it
+ Path path = null;
+ if (Configuration.config.getAbsolute_path().endsWith("/")) {
+ path = Paths.get(Configuration.config.getAbsolute_path() + "prod");
+ } else {
+ path = Paths.get(Configuration.config.getAbsolute_path() + "/prod");
+ }
+
+ if (Files.exists(path) == true) {
+ log.info(new Date() + " - folder '" + path
+ + "' exists, no further action");
+ return true;
+ } else {
+ // create directory and set permissions
+ boolean success = (new File(path + "")).mkdirs();
+
+ if (!success) {
+ log.info(new Date() + " - failed to create folder '" + path
+ + "'");
+ return false;
+ } else {
+ // set permissions
+ try {
+ Runtime.getRuntime().exec("chmod 777 " + path);
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ log.info(new Date() + " - folder '" + path
+ + "' successfully created");
+ return true;
+ }
+ }
+
+ }// end folderProdExists()
+
+
+
+ public String getEncodedSha1Sum(String key) {
+ try {
+ MessageDigest md = MessageDigest.getInstance("SHA1");
+ md.update(key.getBytes());
+ log.info(new Date() + " - successfully returned EncodedSha1Sum");
+ return new BigInteger(1, md.digest()).toString(16);
+ } catch (NoSuchAlgorithmException e) {
+ // handle error case to taste
+ }
+ return null;
+ }
+
+ @Override
+ public long DeleteFtpUser(String user) throws TException {
+ return sql.DeleteUser(user);
+ }
+
+ @Override
+ public String getPathOfImage(String image_id, String version)
+ throws TException {
+ log.info(new Date() + " - successfully returned PathOfImage: "
+ + sql.getPathOfImage(image_id, version));
+ return sql.getPathOfImage(image_id, version);
+
+ }
+
+ @Override
+ public boolean writeVLdata(String imagename, String login,
+ String firstname, String lastname, String university, String Mail,
+ String Tel, String Fak, boolean license, boolean internet,
+ long ram, long cpu, String imagePath, boolean isTemplate,
+ long filesize, long shareMode) throws TException {
+
+ String mode=null;
+
+ if(shareMode==0)
+ {
+ mode="only_local";
+ }
+ else{
+ mode="to_be_published";
+ }
+
+ String pk_institution = sql.setInstitution(university);
+
+ String pk_person = sql.setPerson(login, lastname, firstname, Mail,
+ new Date(), pk_institution);
+
+ sql.setImageData(pk_person, license, internet, cpu, ram, imagename,
+ imagePath, isTemplate, filesize,mode);
+
+ log.info(new Date() + " - written VLdata");
+ // TODO Auto-generated method stub
+ return true;
+ }
+
+ @Override
+ public List<Image> getImageList() throws TException {
+ return sql.getImageList();
+
+ }
+
+ @Override
+ public List<String> getAllOS() throws TException {
+ return sql.getAllOS();
+
+ }
+
+ @Override
+ public Map<String, String> getPersonData(String Vorname, String Nachname)
+ throws TException {
+ return sql.getPersonData(Vorname, Nachname);
+
+ }
+
+ @Override
+ public boolean writeLecturedata(String name, String shortdesc, String desc,
+ String startDate, String endDate, boolean isActive,
+ String imagename, String login, String firstname, String lastname,
+ String university, String Mail, String Tel, String Fak)
+ throws TException {
+ String pk_image = null;
+ Map<String, String> map = new HashMap<String, String>();
+ int imageversion = 0;
+ String pk_institution = sql.setInstitution(university);
+ String pk_person = sql.setPerson(login, lastname, firstname, Mail,
+ new Date(), pk_institution);
+
+ map = sql.getImageIDandVersion(imagename);
+
+ pk_image = map.get("GUID");
+ imageversion = Integer.parseInt(map.get("version"));
+ sql.setLectureData(pk_person, pk_image, imageversion, name, desc,
+ shortdesc, startDate, endDate, isActive);
+ XMLCreator xml = new XMLCreator(sql.getConnection(), name);
+ try {
+ xml.create(name);
+ log.info(new Date() + " - XML created.");
+ } catch (SQLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (ParserConfigurationException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (TransformerException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ @Override
+ public boolean startFileCopy(String filename) throws TException {
+ //copy file from folder temp to folder prod
+ String file = Configuration.config.getAbsolute_path() + "temp/"
+ + filename;
+ File tmpFile = new File(file);
+
+ log.info(new Date()
+ + " - Trying to move file to '/srv/openslx/nfs/prod/"
+ + tmpFile.getName() + "'");
+ try {
+ FileUtils.moveFile(tmpFile,
+ new File(Configuration.config.getAbsolute_path() + "prod/"
+ + filename));
+ // int ret = sql.UpdateImagePath(filename);
+ if (sql.UpdateImagePath(filename) == 0) {
+ log.info(new Date() + " - file moved and database updated.");
+ }
+
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ log.info(new Date() + " - Failed to move file.");
+ e.printStackTrace();
+ }
+ return true;
+ }
+
+ @Override
+ public Map<String, String> getImageData(String imageid, String imageversion)
+ throws TException {
+ log.info(new Date() + " - returning ImageData: "
+ + sql.getImageData(imageid, imageversion).size() + "items.");
+ return sql.getImageData(imageid, imageversion);
+ }
+
+ @Override
+ public boolean updateImageData(String name, String newName,
+ String image_path, boolean license, boolean internet, long ram,
+ long cpu, String id, String version, boolean isTemplate,
+ long filesize, long shareMode) throws TException {
+ String mode=null;
+
+ if(shareMode==0)
+ {
+ mode="only_local";
+ }
+ else{
+ mode="to_be_published";
+ }
+ sql.UpdateImageData(name, newName, image_path, license, internet, cpu,
+ ram, id, version, isTemplate, filesize,mode);
+ return false;
+ }
+
+ @Override
+ public Map<String, String> getLectureData(String lecturename)
+ throws TException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public List<Lecture> getLectureList() throws TException {
+
+
+ log.info(new Date().toString() + " - returning LectureList: "
+ + sql.getLectureList().size() + " items.");
+ return sql.getLectureList();
+
+
+ }
+
+ @Override
+ public boolean updateLecturedata(String name, String newName,
+ String shortdesc, String desc, String startDate, String endDate,
+ boolean isActive, String imagename, String firstname,
+ String lastname, String university, String Mail, String Tel,
+ String Fak, String id) throws TException {
+ String pk_image = "";
+ int imageversion = 0;
+ Map<String, String> image = sql.getImageIDandVersion(imagename);
+
+ pk_image = image.get("GUID");
+ imageversion = Integer.parseInt(image.get("version"));
+ sql.updateLectureData(pk_image, imageversion, lastname, newName, desc,
+ shortdesc, startDate, endDate, isActive, id);
+
+ XMLCreator xml = new XMLCreator(sql.getConnection(), newName);
+ try {
+ xml.create(newName);
+ } catch (SQLException | ParserConfigurationException
+ | TransformerException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ // sql.updateLectureData(con, pk_image, imageversion, lastname, newName,
+ // desc, shortdesc, startDate, endDate, isActive, id);
+ return false;
+ }
+
+ @Override
+ public boolean deleteImageServer(String imageid, String imageversion)
+ throws TException {
+
+ String stringFile = sql.getFile(imageid, imageversion);
+ log.info(new Date() + " - File to Delete: " + stringFile);
+
+ File tmpFile = new File(Configuration.config.getAbsolute_path()
+ + stringFile);
+
+ try {
+ // File wird von Server gel�scht
+ FileUtils.forceDelete(tmpFile);
+ return true;
+
+ } catch (IOException e) {
+ log.info(new Date() + " - Failed to execute deleteImageServer.");
+ e.printStackTrace();
+
+ }
+
+ return false;
+ }
+
+ @Override
+ public boolean deleteImageData(String id, String version) throws TException {
+
+ return sql.deleteImage(id, version);
+ }
+
+ @Override
+ public boolean connectedToLecture(String id, String version)
+ throws TException {
+ // TODO Auto-generated method stub
+ return sql.connectedToLecture(id, version);
+ }
+
+ public boolean deleteLecture(String id, String hs, String user) {
+ Map<String, String> map = new HashMap<String, String>();
+ map = sql.getDeleteXMLData(id);
+ try {
+
+ String path = Configuration.config.getAbsolute_path()
+ + "temp/"
+ + map.get("date")
+ .substring(0, map.get("date").length() - 2) + "_"
+ + hs + "_" + user + "_" + map.get("name") + ".xml";
+ File xmlFile = new File(path);
+ FileUtils.forceDelete(xmlFile);
+ } catch (IOException e) {
+ log.info(new Date() + " - Failed to execute deleteLecture");
+ e.printStackTrace();
+ }
+ return sql.deleteLecture(id);
+
+ }
+
+}