diff options
author | Jonathan Bauer | 2017-09-06 13:48:33 +0200 |
---|---|---|
committer | Jonathan Bauer | 2017-09-06 13:48:33 +0200 |
commit | e310fa0738149f20b9de6b173d3d175857b0c748 (patch) | |
tree | 5857811d25cc53c51590cc0158ec84e049a3ed88 /dozentenmodulserver/src/main/java/org/openslx | |
parent | [client] Update apache httpclient, pass custom client to ecp-client-lean (diff) | |
download | tutor-module-e310fa0738149f20b9de6b173d3d175857b0c748.tar.gz tutor-module-e310fa0738149f20b9de6b173d3d175857b0c748.tar.xz tutor-module-e310fa0738149f20b9de6b173d3d175857b0c748.zip |
[client/server] network share feature [WIP]
Diffstat (limited to 'dozentenmodulserver/src/main/java/org/openslx')
4 files changed, 112 insertions, 2 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/SupportedFeatures.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/SupportedFeatures.java index 73d1a788..203f3de1 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/SupportedFeatures.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/SupportedFeatures.java @@ -8,6 +8,7 @@ public class SupportedFeatures { static { registerFeature(Feature.EXTEND_EXPIRED_VM); + registerFeature(Feature.NETWORK_SHARES); } public static String getFeatureString() { diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java index ff160917..58c7bdb2 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java @@ -24,6 +24,7 @@ import org.openslx.bwlp.thrift.iface.LectureRead; import org.openslx.bwlp.thrift.iface.LectureSummary; import org.openslx.bwlp.thrift.iface.LectureWrite; import org.openslx.bwlp.thrift.iface.NetRule; +import org.openslx.bwlp.thrift.iface.NetShare; import org.openslx.bwlp.thrift.iface.TNotFoundException; import org.openslx.bwlp.thrift.iface.UserInfo; import org.openslx.util.Json; @@ -39,6 +40,7 @@ public class DbLecture { static { Json.registerThriftClass(NetRule.class); + Json.registerThriftClass(NetShare.class); } private static void setWriteFields(MysqlStatement stmt, String lectureId, LectureWrite lecture, @@ -117,6 +119,7 @@ public class DbLecture { stmt.setString("ownerid", user.userId); stmt.executeUpdate(); writeLocations(connection, lectureId, lecture.locationIds); + DbNetshare.writeNetworkShares(connection, lectureId, lecture.networkShares); connection.commit(); return lectureId; } catch (SQLException e) { @@ -138,6 +141,7 @@ public class DbLecture { + " WHERE lectureid = :lectureid"); setWriteFields(stmt, lectureId, lecture, user); writeLocations(connection, lectureId, lecture.locationIds); + DbNetshare.writeNetworkShares(connection, lectureId, lecture.networkShares); stmt.executeUpdate(); } @@ -274,10 +278,11 @@ public class DbLecture { + " l.autoupdate, l.isenabled, l.starttime, l.endtime, l.lastused, l.usecount, l.createtime," + " l.updatetime, l.ownerid, l.updaterid, l.runscript, l.nics, l.netrules, l.isexam," + " l.isprivate, l.islocationprivate, l.hasinternetaccess, l.hasusbaccess," - + " l.caneditdefault, l.canadmindefault, p.canedit, p.canadmin" + + " l.caneditdefault, l.canadmindefault, p.canedit, p.canadmin, n.sharedata" + " FROM lecture l " + " LEFT JOIN imageversion i USING (imageversionid)" + " LEFT JOIN lecturepermission p ON (l.lectureid = p.lectureid AND p.userid = :userid)" + + " LEFT JOIN networkshare n ON (l.lectureid = n.lectureid)" + " WHERE l.lectureid = :lectureid LIMIT 1"); stmt.setString("userid", user == null ? "" : user.userId); stmt.setString("lectureid", lectureId); @@ -323,6 +328,7 @@ public class DbLecture { lecture.setUserPermissions(DbLecturePermissions.fromResultSetUser(rs)); User.setCombinedUserPermissions(lecture, user); lecture.setLocationIds(DbLocation.getLectureLocations(connection, lectureId)); + lecture.setNetworkShares(DbNetshare.getLectureNetshares(connection, lectureId)); return lecture; } catch (SQLException e) { LOGGER.error("Query failed in DbLecture.getLectureDetails()", e); diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbNetshare.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbNetshare.java new file mode 100644 index 00000000..af8b3fdc --- /dev/null +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbNetshare.java @@ -0,0 +1,61 @@ +package org.openslx.bwlp.sat.database.mappers; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.log4j.Logger; +import org.openslx.bwlp.sat.database.Database; +import org.openslx.bwlp.sat.database.MysqlConnection; +import org.openslx.bwlp.sat.database.MysqlStatement; +import org.openslx.bwlp.thrift.iface.NetShare; +import org.openslx.util.Json; + +public class DbNetshare { + + private static final Logger LOGGER = Logger.getLogger(DbNetshare.class); + + public static void writeNetworkShares(MysqlConnection connection, String lectureId, List<NetShare> shares) + throws SQLException { + if (shares == null || shares.isEmpty() || lectureId == null || lectureId.isEmpty()) { + return; + } + MysqlStatement delStmt = connection.prepareStatement("DELETE FROM networkshare WHERE lectureid = :lectureid"); + delStmt.setString("lectureid", lectureId); + delStmt.executeUpdate(); + + MysqlStatement addStmt = connection + .prepareStatement("INSERT IGNORE INTO networkshare (shareid, lectureid, shareuid, sharedata)" + + " VALUES (DEFAULT, :lectureid, NULL, :sharedata)"); + addStmt.setString("lectureid", lectureId); + for (NetShare share : shares) { + String netshareJson = Json.serialize(share); + addStmt.setString("sharedata", netshareJson); + addStmt.executeUpdate(); + } + } + + public static List<NetShare> getLectureNetshares(String lectureId) throws SQLException { + try (MysqlConnection connection = Database.getConnection()) { + return getLectureNetshares(connection, lectureId); + } catch (SQLException e) { + LOGGER.error("Query failed in DbLecture.getLectureDetails()", e); + throw e; + } + } + + public static List<NetShare> getLectureNetshares(MysqlConnection connection, String lectureId) throws SQLException { + List<NetShare> list = new ArrayList<>(); + MysqlStatement netsharestmt = connection + .prepareStatement("SELECT sharedata FROM networkshare WHERE lectureid = :lectureid"); + netsharestmt.setString("lectureid", lectureId); + ResultSet rs = netsharestmt.executeQuery(); + while (rs.next()) { + // TODO get the shares saved on slx-admin, if any shareuid aren't null + list.add(Json.deserializeThrift(rs.getString("sharedata"), NetShare.class)); + } + return list; + } + +} diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java index e369f162..111578d3 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java @@ -3,14 +3,18 @@ package org.openslx.bwlp.sat.web; import java.io.ByteArrayInputStream; import java.io.IOException; import java.sql.SQLException; +import java.util.List; import java.util.Map; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.log4j.Logger; import org.openslx.bwlp.sat.database.mappers.DbLecture; +import org.openslx.bwlp.sat.database.mappers.DbNetshare; import org.openslx.bwlp.sat.fileserv.FileServer; import org.openslx.bwlp.thrift.iface.LectureRead; import org.openslx.bwlp.thrift.iface.NetRule; +import org.openslx.bwlp.thrift.iface.NetShare; +import org.openslx.bwlp.thrift.iface.NetShareAuth; import org.openslx.bwlp.thrift.iface.TNotFoundException; import org.openslx.util.Json; import org.openslx.util.vm.VmMetaData; @@ -94,6 +98,8 @@ public class WebServer extends NanoHTTPD { return serveLectureStart(parts[2]); if (parts[3].equals("netrules")) return serveLectureNetRules(parts[2]); + if (parts[3].equals("netshares")) + return serveLectureNetshares(parts[2]); if (parts[3].equals("runscript")) return serveLectureScript(parts[2]); } @@ -188,6 +194,43 @@ public class WebServer extends NanoHTTPD { lecture.runscript); } + private Response serveLectureNetshares(String lectureId) { + List<NetShare> list = null; + try { + list = DbNetshare.getLectureNetshares(lectureId); + // TODO handling not found necessary? + } catch (SQLException e) { + return internalServerError(); + } + // TODO format expected from the client-side to + // handle different types of login. Currently: + // * no username nor password => use credentials of logged in user + // * username set, no password => guest mode + // * username and password set => user-specificed credentials + StringBuilder sb = new StringBuilder(); + if (!list.isEmpty()) { + for (NetShare share : list) { + sb.append(share.path); + if (share.auth == NetShareAuth.LOGIN_USER) { + // nothing to do + } + if (share.auth == NetShareAuth.GUEST_USER) { + sb.append(' '); + sb.append(share.username); + } + if (share.auth == NetShareAuth.SPECIAL_USER) { + sb.append(' '); + sb.append(share.username); + sb.append(' '); + sb.append(share.password); // TODO fixme + } + sb.append("\n"); + } + } + return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "text/plain; charset=utf-8", + sb.toString()); + } + /** * Return full list of lectures matching given location(s). * @@ -229,5 +272,4 @@ public class WebServer extends NanoHTTPD { } return new NanoHTTPD.Response(NanoHTTPD.Response.Status.BAD_REQUEST, "text/plain", message); } - } |