diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..a6f89c2d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target/ \ No newline at end of file diff --git a/pom.xml b/pom.xml index 38d611a8..5cb09e1c 100644 --- a/pom.xml +++ b/pom.xml @@ -3,11 +3,11 @@ 4.0.0 uk.ac.dundee.computing.aec - Instagrim + SaveTheSemester 1.0-SNAPSHOT war - Instagrim + SaveTheSemester ${project.build.directory}/endorsed diff --git a/src/main/java/uk/ac/dundee/computing/aec/instagrim/lib/AeSimpleSHA1.java b/src/main/java/uk/ac/dundee/computing/aec/instagrim/lib/AeSimpleSHA1.java deleted file mode 100644 index e1fe6653..00000000 --- a/src/main/java/uk/ac/dundee/computing/aec/instagrim/lib/AeSimpleSHA1.java +++ /dev/null @@ -1,35 +0,0 @@ -package uk.ac.dundee.computing.aec.instagrim.lib; - -import java.io.UnsupportedEncodingException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -public class AeSimpleSHA1 { - - private static String convertToHex(byte[] data) { - StringBuffer buf = new StringBuffer(); - for (int i = 0; i < data.length; i++) { - int halfbyte = (data[i] >>> 4) & 0x0F; - int two_halfs = 0; - do { - if ((0 <= halfbyte) && (halfbyte <= 9)) - buf.append((char) ('0' + halfbyte)); - else - buf.append((char) ('a' + (halfbyte - 10))); - halfbyte = data[i] & 0x0F; - } while(two_halfs++ < 1); - } - return buf.toString(); - } - - public static String SHA1(String text) - throws NoSuchAlgorithmException, UnsupportedEncodingException { - MessageDigest md; - md = MessageDigest.getInstance("SHA-1"); - byte[] sha1hash = new byte[40]; - md.update(text.getBytes("iso-8859-1"), 0, text.length()); - sha1hash = md.digest(); - return convertToHex(sha1hash); - } -} - \ No newline at end of file diff --git a/src/main/java/uk/ac/dundee/computing/aec/instagrim/lib/CassandraHosts.java b/src/main/java/uk/ac/dundee/computing/aec/instagrim/lib/CassandraHosts.java deleted file mode 100644 index d748decd..00000000 --- a/src/main/java/uk/ac/dundee/computing/aec/instagrim/lib/CassandraHosts.java +++ /dev/null @@ -1,74 +0,0 @@ -package uk.ac.dundee.computing.aec.instagrim.lib; - -import com.datastax.driver.core.*; - -import java.util.Iterator; -import java.util.Set; -import java.util.ArrayList; -import java.util.List; - -/** - * ******************************************************** - * - * - * @author administrator - * - * Hosts are 192.168.2.10 Seed for Vagrant hosts - * - * - * - * - */ -public final class CassandraHosts { - - private static Cluster cluster; - static String Host = "127.0.0.1"; //at least one starting point to talk to - - public CassandraHosts() { - - } - - public static String getHost() { - return (Host); - } - - public static String[] getHosts(Cluster cluster) { - - if (cluster == null) { - System.out.println("Creating cluster connection"); - cluster = Cluster.builder().addContactPoint(Host).build(); - } - System.out.println("Cluster Name " + cluster.getClusterName()); - Metadata mdata = cluster.getMetadata(); - Set hosts = mdata.getAllHosts(); - String sHosts[] = new String[hosts.size()]; - - Iterator it = hosts.iterator(); - int i = 0; - while (it.hasNext()) { - Host ch = it.next(); - sHosts[i] = (String) ch.getAddress().toString(); - - System.out.println("Hosts" + ch.getAddress().toString()); - i++; - } - - return sHosts; - } - - public static Cluster getCluster() { - System.out.println("getCluster"); - cluster = Cluster.builder() - .addContactPoint(Host).build(); - getHosts(cluster); - Keyspaces.SetUpKeySpaces(cluster); - - return cluster; - - } - - public void close() { - cluster.close(); - } - -} diff --git a/src/main/java/uk/ac/dundee/computing/aec/instagrim/lib/Convertors.java b/src/main/java/uk/ac/dundee/computing/aec/instagrim/lib/Convertors.java deleted file mode 100644 index 3508604f..00000000 --- a/src/main/java/uk/ac/dundee/computing/aec/instagrim/lib/Convertors.java +++ /dev/null @@ -1,172 +0,0 @@ -package uk.ac.dundee.computing.aec.instagrim.lib; - -import java.net.URLDecoder; -import java.util.StringTokenizer; -//import java.util.UUID; -import javax.servlet.http.HttpServletRequest; -import com.eaio.uuid.UUID; - -public final class Convertors { - public static int DISPLAY_IMAGE=0; - public static int DISPLAY_THUMB=1; - public static int DISPLAY_PROCESSED=2; - public void Convertors() { - - } - - public static java.util.UUID getTimeUUID() { - return java.util.UUID.fromString(new com.eaio.uuid.UUID().toString()); - } - - - - public static byte[] asByteArray(java.util.UUID uuid) { - - long msb = uuid.getMostSignificantBits(); - long lsb = uuid.getLeastSignificantBits(); - byte[] buffer = new byte[16]; - - for (int i = 0; i < 8; i++) { - buffer[i] = (byte) (msb >>> 8 * (7 - i)); - } - for (int i = 8; i < 16; i++) { - buffer[i] = (byte) (lsb >>> 8 * (7 - i)); - } - - return buffer; - } - - public static byte[] longToByteArray(long value) { - byte[] buffer = new byte[8]; //longs are 8 bytes I believe - for (int i = 7; i >= 0; i--) { //fill from the right - buffer[i] = (byte) (value & 0x00000000000000ff); //get the bottom byte - - //System.out.print(""+Integer.toHexString((int)buffer[i])+","); - value = value >>> 8; //Shift the value right 8 bits - } - return buffer; - } - - public static long byteArrayToLong(byte[] buffer) { - long value = 0; - long multiplier = 1; - for (int i = 7; i >= 0; i--) { //get from the right - - //System.out.println(Long.toHexString(multiplier)+"\t"+Integer.toHexString((int)buffer[i])); - value = value + (buffer[i] & 0xff) * multiplier; // add the value * the hex mulitplier - multiplier = multiplier << 8; - } - return value; - } - - public static void displayByteArrayAsHex(byte[] buffer) { - int byteArrayLength = buffer.length; - for (int i = 0; i < byteArrayLength; i++) { - int val = (int) buffer[i]; - // System.out.print(Integer.toHexString(val)+","); - } - - //System.out.println(); - } - -//From: http://www.captain.at/howto-java-convert-binary-data.php - public static long arr2long(byte[] arr, int start) { - int i = 0; - int len = 4; - int cnt = 0; - byte[] tmp = new byte[len]; - for (i = start; i < (start + len); i++) { - tmp[cnt] = arr[i]; - cnt++; - } - long accum = 0; - i = 0; - for (int shiftBy = 0; shiftBy < 32; shiftBy += 8) { - accum |= ((long) (tmp[i] & 0xff)) << shiftBy; - i++; - } - return accum; - } - - public static String[] SplitTags(String Tags) { - String args[] = null; - - StringTokenizer st = Convertors.SplitTagString(Tags); - args = new String[st.countTokens() + 1]; //+1 for _No_Tag_ - //Lets assume the number is the last argument - - int argv = 0; - while (st.hasMoreTokens()) {; - args[argv] = new String(); - args[argv] = st.nextToken(); - argv++; - } - args[argv] = "_No-Tag_"; - return args; - } - - private static StringTokenizer SplitTagString(String str) { - return new StringTokenizer(str, ","); - - } - - public static String[] SplitFiletype(String type) { - String args[] = null; - - StringTokenizer st = SplitString(type); - args = new String[st.countTokens()]; - //Lets assume the number is the last argument - - int argv = 0; - while (st.hasMoreTokens()) {; - args[argv] = new String(); - - args[argv] = st.nextToken(); - try { - //System.out.println("String was "+URLDecoder.decode(args[argv],"UTF-8")); - args[argv] = URLDecoder.decode(args[argv], "UTF-8"); - - } catch (Exception et) { - System.out.println("Bad URL Encoding" + args[argv]); - } - argv++; - } - - //so now they'll be in the args array. - // argv[0] should be the user directory - return args; - } - - public static String[] SplitRequestPath(HttpServletRequest request) { - String args[] = null; - - StringTokenizer st = SplitString(request.getRequestURI()); - args = new String[st.countTokens()]; - //Lets assume the number is the last argument - - int argv = 0; - while (st.hasMoreTokens()) {; - args[argv] = new String(); - - args[argv] = st.nextToken(); - try { - //System.out.println("String was "+URLDecoder.decode(args[argv],"UTF-8")); - args[argv] = URLDecoder.decode(args[argv], "UTF-8"); - - } catch (Exception et) { - System.out.println("Bad URL Encoding" + args[argv]); - } - argv++; - } - - //so now they'll be in the args array. - // argv[0] should be the user directory - return args; - } - - private static StringTokenizer SplitString(String str) { - return new StringTokenizer(str, "/"); - - } - -} diff --git a/src/main/java/uk/ac/dundee/computing/aec/instagrim/lib/Keyspaces.java b/src/main/java/uk/ac/dundee/computing/aec/instagrim/lib/Keyspaces.java deleted file mode 100644 index 48988e36..00000000 --- a/src/main/java/uk/ac/dundee/computing/aec/instagrim/lib/Keyspaces.java +++ /dev/null @@ -1,103 +0,0 @@ -package uk.ac.dundee.computing.aec.instagrim.lib; - -import java.util.ArrayList; -import java.util.List; - -import com.datastax.driver.core.*; - -public final class Keyspaces { - - public Keyspaces() { - - } - - public static void SetUpKeySpaces(Cluster c) { - try { - //Add some keyspaces here - String createkeyspace = "create keyspace if not exists instagrim WITH replication = {'class':'SimpleStrategy', 'replication_factor':1}"; - String CreatePicTable = "CREATE TABLE if not exists instagrim.Pics (" - + " user varchar," - + " picid uuid, " - + " interaction_time timestamp," - + " title varchar," - + " image blob," - + " thumb blob," - + " processed blob," - + " imagelength int," - + " thumblength int," - + " processedlength int," - + " type varchar," - + " name varchar," - + " PRIMARY KEY (picid)" - + ")"; - String Createuserpiclist = "CREATE TABLE if not exists instagrim.userpiclist (\n" - + "picid uuid,\n" - + "user varchar,\n" - + "pic_added timestamp,\n" - + "PRIMARY KEY (user,pic_added)\n" - + ") WITH CLUSTERING ORDER BY (pic_added desc);"; - String CreateAddressType = "CREATE TYPE if not exists instagrim.address (\n" - + " street text,\n" - + " city text,\n" - + " zip int\n" - + " );"; - String CreateUserProfile = "CREATE TABLE if not exists instagrim.userprofiles (\n" - + " login text PRIMARY KEY,\n" - + " password text,\n" - + " first_name text,\n" - + " last_name text,\n" - + " email set,\n" - + " addresses map>\n" - + " );"; - Session session = c.connect(); - try { - PreparedStatement statement = session - .prepare(createkeyspace); - BoundStatement boundStatement = new BoundStatement( - statement); - ResultSet rs = session - .execute(boundStatement); - System.out.println("created instagrim "); - } catch (Exception et) { - System.out.println("Can't create instagrim " + et); - } - - //now add some column families - System.out.println("" + CreatePicTable); - - try { - SimpleStatement cqlQuery = new SimpleStatement(CreatePicTable); - session.execute(cqlQuery); - } catch (Exception et) { - System.out.println("Can't create tweet table " + et); - } - System.out.println("" + Createuserpiclist); - - try { - SimpleStatement cqlQuery = new SimpleStatement(Createuserpiclist); - session.execute(cqlQuery); - } catch (Exception et) { - System.out.println("Can't create user pic list table " + et); - } - System.out.println("" + CreateAddressType); - try { - SimpleStatement cqlQuery = new SimpleStatement(CreateAddressType); - session.execute(cqlQuery); - } catch (Exception et) { - System.out.println("Can't create Address type " + et); - } - System.out.println("" + CreateUserProfile); - try { - SimpleStatement cqlQuery = new SimpleStatement(CreateUserProfile); - session.execute(cqlQuery); - } catch (Exception et) { - System.out.println("Can't create Address Profile " + et); - } - session.close(); - - } catch (Exception et) { - System.out.println("Other keyspace or coulm definition error" + et); - } - - } -} diff --git a/src/main/java/uk/ac/dundee/computing/aec/instagrim/models/PicModel.java b/src/main/java/uk/ac/dundee/computing/aec/instagrim/models/PicModel.java deleted file mode 100644 index 79d56975..00000000 --- a/src/main/java/uk/ac/dundee/computing/aec/instagrim/models/PicModel.java +++ /dev/null @@ -1,216 +0,0 @@ -package uk.ac.dundee.computing.aec.instagrim.models; - -/* - * Expects a cassandra columnfamily defined as - * use keyspace2; - CREATE TABLE Tweets ( - user varchar, - interaction_time timeuuid, - tweet varchar, - PRIMARY KEY (user,interaction_time) - ) WITH CLUSTERING ORDER BY (interaction_time DESC); - * To manually generate a UUID use: - * http://www.famkruithof.net/uuid/uuidgen - */ -import com.datastax.driver.core.BoundStatement; -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.PreparedStatement; -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; -import com.datastax.driver.core.Session; -import com.datastax.driver.core.utils.Bytes; -import java.awt.image.BufferedImage; -import java.io.ByteArrayOutputStream; -import java.io.File; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.ByteBuffer; -import java.util.Date; -import java.util.LinkedList; -import javax.imageio.ImageIO; -import static org.imgscalr.Scalr.*; -import org.imgscalr.Scalr.Method; - -import uk.ac.dundee.computing.aec.instagrim.lib.*; -import uk.ac.dundee.computing.aec.instagrim.stores.Pic; -//import uk.ac.dundee.computing.aec.stores.TweetStore; - -public class PicModel { - - Cluster cluster; - - public void PicModel() { - - } - - public void setCluster(Cluster cluster) { - this.cluster = cluster; - } - - public void insertPic(byte[] b, String type, String name, String user) { - try { - Convertors convertor = new Convertors(); - - String types[]=Convertors.SplitFiletype(type); - ByteBuffer buffer = ByteBuffer.wrap(b); - int length = b.length; - java.util.UUID picid = convertor.getTimeUUID(); - - //The following is a quick and dirty way of doing this, will fill the disk quickly ! - Boolean success = (new File("/var/tmp/instagrim/")).mkdirs(); - FileOutputStream output = new FileOutputStream(new File("/var/tmp/instagrim/" + picid)); - - output.write(b); - byte [] thumbb = picresize(picid.toString(),types[1]); - int thumblength= thumbb.length; - ByteBuffer thumbbuf=ByteBuffer.wrap(thumbb); - byte[] processedb = picdecolour(picid.toString(),types[1]); - ByteBuffer processedbuf=ByteBuffer.wrap(processedb); - int processedlength=processedb.length; - Session session = cluster.connect("instagrim"); - - PreparedStatement psInsertPic = session.prepare("insert into pics ( picid, image,thumb,processed, user, interaction_time,imagelength,thumblength,processedlength,type,name) values(?,?,?,?,?,?,?,?,?,?,?)"); - PreparedStatement psInsertPicToUser = session.prepare("insert into userpiclist ( picid, user, pic_added) values(?,?,?)"); - BoundStatement bsInsertPic = new BoundStatement(psInsertPic); - BoundStatement bsInsertPicToUser = new BoundStatement(psInsertPicToUser); - - Date DateAdded = new Date(); - session.execute(bsInsertPic.bind(picid, buffer, thumbbuf,processedbuf, user, DateAdded, length,thumblength,processedlength, type, name)); - session.execute(bsInsertPicToUser.bind(picid, user, DateAdded)); - session.close(); - - } catch (IOException ex) { - System.out.println("Error --> " + ex); - } - } - - public byte[] picresize(String picid,String type) { - try { - BufferedImage BI = ImageIO.read(new File("/var/tmp/instagrim/" + picid)); - BufferedImage thumbnail = createThumbnail(BI); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ImageIO.write(thumbnail, type, baos); - baos.flush(); - - byte[] imageInByte = baos.toByteArray(); - baos.close(); - return imageInByte; - } catch (IOException et) { - - } - return null; - } - - public byte[] picdecolour(String picid,String type) { - try { - BufferedImage BI = ImageIO.read(new File("/var/tmp/instagrim/" + picid)); - BufferedImage processed = createProcessed(BI); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ImageIO.write(processed, type, baos); - baos.flush(); - byte[] imageInByte = baos.toByteArray(); - baos.close(); - return imageInByte; - } catch (IOException et) { - - } - return null; - } - - public static BufferedImage createThumbnail(BufferedImage img) { - img = resize(img, Method.SPEED, 250, OP_ANTIALIAS, OP_GRAYSCALE); - // Let's add a little border before we return result. - return pad(img, 2); - } - - public static BufferedImage createProcessed(BufferedImage img) { - int Width=img.getWidth()-1; - img = resize(img, Method.SPEED, Width, OP_ANTIALIAS, OP_GRAYSCALE); - return pad(img, 4); - } - - public java.util.LinkedList getPicsForUser(String User) { - java.util.LinkedList Pics = new java.util.LinkedList<>(); - Session session = cluster.connect("instagrim"); - PreparedStatement ps = session.prepare("select picid from userpiclist where user =?"); - ResultSet rs = null; - BoundStatement boundStatement = new BoundStatement(ps); - rs = session.execute( // this is where the query is executed - boundStatement.bind( // here you are binding the 'boundStatement' - User)); - if (rs.isExhausted()) { - System.out.println("No Images returned"); - return null; - } else { - for (Row row : rs) { - Pic pic = new Pic(); - java.util.UUID UUID = row.getUUID("picid"); - System.out.println("UUID" + UUID.toString()); - pic.setUUID(UUID); - Pics.add(pic); - - } - } - return Pics; - } - - public Pic getPic(int image_type, java.util.UUID picid) { - Session session = cluster.connect("instagrim"); - ByteBuffer bImage = null; - String type = null; - int length = 0; - try { - Convertors convertor = new Convertors(); - ResultSet rs = null; - PreparedStatement ps = null; - - if (image_type == Convertors.DISPLAY_IMAGE) { - - ps = session.prepare("select image,imagelength,type from pics where picid =?"); - } else if (image_type == Convertors.DISPLAY_THUMB) { - ps = session.prepare("select thumb,imagelength,thumblength,type from pics where picid =?"); - } else if (image_type == Convertors.DISPLAY_PROCESSED) { - ps = session.prepare("select processed,processedlength,type from pics where picid =?"); - } - BoundStatement boundStatement = new BoundStatement(ps); - rs = session.execute( // this is where the query is executed - boundStatement.bind( // here you are binding the 'boundStatement' - picid)); - - if (rs.isExhausted()) { - System.out.println("No Images returned"); - return null; - } else { - for (Row row : rs) { - if (image_type == Convertors.DISPLAY_IMAGE) { - bImage = row.getBytes("image"); - length = row.getInt("imagelength"); - } else if (image_type == Convertors.DISPLAY_THUMB) { - bImage = row.getBytes("thumb"); - length = row.getInt("thumblength"); - - } else if (image_type == Convertors.DISPLAY_PROCESSED) { - bImage = row.getBytes("processed"); - length = row.getInt("processedlength"); - } - - type = row.getString("type"); - - } - } - } catch (Exception et) { - System.out.println("Can't get Pic" + et); - return null; - } - session.close(); - Pic p = new Pic(); - p.setPic(bImage, length, type); - - return p; - - } - -} diff --git a/src/main/java/uk/ac/dundee/computing/aec/instagrim/models/User.java b/src/main/java/uk/ac/dundee/computing/aec/instagrim/models/User.java deleted file mode 100644 index f8ceb04f..00000000 --- a/src/main/java/uk/ac/dundee/computing/aec/instagrim/models/User.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - -package uk.ac.dundee.computing.aec.instagrim.models; - -import com.datastax.driver.core.BoundStatement; -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.PreparedStatement; -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; -import com.datastax.driver.core.Session; -import java.io.UnsupportedEncodingException; -import java.security.NoSuchAlgorithmException; -import uk.ac.dundee.computing.aec.instagrim.lib.AeSimpleSHA1; -import uk.ac.dundee.computing.aec.instagrim.stores.Pic; - -/** - * - * @author Administrator - */ -public class User { - Cluster cluster; - public User(){ - - } - - public boolean RegisterUser(String username, String Password){ - AeSimpleSHA1 sha1handler= new AeSimpleSHA1(); - String EncodedPassword=null; - try { - EncodedPassword= sha1handler.SHA1(Password); - }catch (UnsupportedEncodingException | NoSuchAlgorithmException et){ - System.out.println("Can't check your password"); - return false; - } - Session session = cluster.connect("instagrim"); - PreparedStatement ps = session.prepare("insert into userprofiles (login,password) Values(?,?)"); - - BoundStatement boundStatement = new BoundStatement(ps); - session.execute( // this is where the query is executed - boundStatement.bind( // here you are binding the 'boundStatement' - username,EncodedPassword)); - //We are assuming this always works. Also a transaction would be good here ! - - return true; - } - - public boolean IsValidUser(String username, String Password){ - AeSimpleSHA1 sha1handler= new AeSimpleSHA1(); - String EncodedPassword=null; - try { - EncodedPassword= sha1handler.SHA1(Password); - }catch (UnsupportedEncodingException | NoSuchAlgorithmException et){ - System.out.println("Can't check your password"); - return false; - } - Session session = cluster.connect("instagrim"); - PreparedStatement ps = session.prepare("select password from userprofiles where login =?"); - ResultSet rs = null; - BoundStatement boundStatement = new BoundStatement(ps); - rs = session.execute( // this is where the query is executed - boundStatement.bind( // here you are binding the 'boundStatement' - username)); - if (rs.isExhausted()) { - System.out.println("No Images returned"); - return false; - } else { - for (Row row : rs) { - - String StoredPass = row.getString("password"); - if (StoredPass.compareTo(EncodedPassword) == 0) - return true; - } - } - - - return false; - } - public void setCluster(Cluster cluster) { - this.cluster = cluster; - } - - -} diff --git a/src/main/java/uk/ac/dundee/computing/aec/instagrim/servlets/Image.java b/src/main/java/uk/ac/dundee/computing/aec/instagrim/servlets/Image.java deleted file mode 100644 index 0c5cfe18..00000000 --- a/src/main/java/uk/ac/dundee/computing/aec/instagrim/servlets/Image.java +++ /dev/null @@ -1,168 +0,0 @@ -package uk.ac.dundee.computing.aec.instagrim.servlets; - -import com.datastax.driver.core.Cluster; -import java.io.BufferedInputStream; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.PrintWriter; -import java.util.HashMap; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletConfig; -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.HttpSession; -import javax.servlet.http.Part; -import org.apache.commons.fileupload.FileItemIterator; -import org.apache.commons.fileupload.FileItemStream; -import org.apache.commons.fileupload.servlet.ServletFileUpload; -import org.apache.commons.fileupload.util.Streams; -import uk.ac.dundee.computing.aec.instagrim.lib.CassandraHosts; -import uk.ac.dundee.computing.aec.instagrim.lib.Convertors; -import uk.ac.dundee.computing.aec.instagrim.models.PicModel; -import uk.ac.dundee.computing.aec.instagrim.stores.LoggedIn; -import uk.ac.dundee.computing.aec.instagrim.stores.Pic; - -/** - * Servlet implementation class Image - */ -@WebServlet(urlPatterns = { - "/Image", - "/Image/*", - "/Thumb/*", - "/Images", - "/Images/*" -}) -@MultipartConfig - -public class Image extends HttpServlet { - - private static final long serialVersionUID = 1L; - private Cluster cluster; - private HashMap CommandsMap = new HashMap(); - - - - /** - * @see HttpServlet#HttpServlet() - */ - public Image() { - super(); - // TODO Auto-generated constructor stub - CommandsMap.put("Image", 1); - CommandsMap.put("Images", 2); - CommandsMap.put("Thumb", 3); - - } - - public void init(ServletConfig config) throws ServletException { - // TODO Auto-generated method stub - cluster = CassandraHosts.getCluster(); - } - - /** - * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse - * response) - */ - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // TODO Auto-generated method stub - String args[] = Convertors.SplitRequestPath(request); - int command; - try { - command = (Integer) CommandsMap.get(args[1]); - } catch (Exception et) { - error("Bad Operator", response); - return; - } - switch (command) { - case 1: - DisplayImage(Convertors.DISPLAY_PROCESSED,args[2], response); - break; - case 2: - DisplayImageList(args[2], request, response); - break; - case 3: - DisplayImage(Convertors.DISPLAY_THUMB,args[2], response); - break; - default: - error("Bad Operator", response); - } - } - - private void DisplayImageList(String User, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - PicModel tm = new PicModel(); - tm.setCluster(cluster); - java.util.LinkedList lsPics = tm.getPicsForUser(User); - RequestDispatcher rd = request.getRequestDispatcher("/UsersPics.jsp"); - request.setAttribute("Pics", lsPics); - rd.forward(request, response); - - } - - private void DisplayImage(int type,String Image, HttpServletResponse response) throws ServletException, IOException { - PicModel tm = new PicModel(); - tm.setCluster(cluster); - - - Pic p = tm.getPic(type,java.util.UUID.fromString(Image)); - - OutputStream out = response.getOutputStream(); - - response.setContentType(p.getType()); - response.setContentLength(p.getLength()); - //out.write(Image); - InputStream is = new ByteArrayInputStream(p.getBytes()); - BufferedInputStream input = new BufferedInputStream(is); - byte[] buffer = new byte[8192]; - for (int length = 0; (length = input.read(buffer)) > 0;) { - out.write(buffer, 0, length); - } - out.close(); - } - - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - for (Part part : request.getParts()) { - System.out.println("Part Name " + part.getName()); - - String type = part.getContentType(); - String filename = part.getSubmittedFileName(); - - InputStream is = request.getPart(part.getName()).getInputStream(); - int i = is.available(); - HttpSession session=request.getSession(); - LoggedIn lg= (LoggedIn)session.getAttribute("LoggedIn"); - String username="majed"; - if (lg.getlogedin()){ - username=lg.getUsername(); - } - if (i > 0) { - byte[] b = new byte[i + 1]; - is.read(b); - System.out.println("Length : " + b.length); - PicModel tm = new PicModel(); - tm.setCluster(cluster); - tm.insertPic(b, type, filename, username); - - is.close(); - } - RequestDispatcher rd = request.getRequestDispatcher("/upload.jsp"); - rd.forward(request, response); - } - - } - - private void error(String mess, HttpServletResponse response) throws ServletException, IOException { - - PrintWriter out = null; - out = new PrintWriter(response.getOutputStream()); - out.println("

You have a na error in your input

"); - out.println("

" + mess + "

"); - out.close(); - return; - } -} diff --git a/src/main/java/uk/ac/dundee/computing/aec/instagrim/servlets/Login.java b/src/main/java/uk/ac/dundee/computing/aec/instagrim/servlets/Login.java deleted file mode 100644 index bb298456..00000000 --- a/src/main/java/uk/ac/dundee/computing/aec/instagrim/servlets/Login.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - -package uk.ac.dundee.computing.aec.instagrim.servlets; - -import com.datastax.driver.core.Cluster; -import java.io.IOException; -import java.io.PrintWriter; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import uk.ac.dundee.computing.aec.instagrim.lib.CassandraHosts; -import uk.ac.dundee.computing.aec.instagrim.models.User; -import uk.ac.dundee.computing.aec.instagrim.stores.LoggedIn; - -/** - * - * @author Administrator - */ -@WebServlet(name = "Login", urlPatterns = {"/Login"}) -public class Login extends HttpServlet { - - Cluster cluster=null; - - - public void init(ServletConfig config) throws ServletException { - // TODO Auto-generated method stub - cluster = CassandraHosts.getCluster(); - } - - /** - * Handles the HTTP POST method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - - String username=request.getParameter("username"); - String password=request.getParameter("password"); - - User us=new User(); - us.setCluster(cluster); - boolean isValid=us.IsValidUser(username, password); - HttpSession session=request.getSession(); - System.out.println("Session in servlet "+session); - if (isValid){ - LoggedIn lg= new LoggedIn(); - lg.setLogedin(); - lg.setUsername(username); - //request.setAttribute("LoggedIn", lg); - - session.setAttribute("LoggedIn", lg); - System.out.println("Session in servlet "+session); - RequestDispatcher rd=request.getRequestDispatcher("index.jsp"); - rd.forward(request,response); - - }else{ - response.sendRedirect("/Instagrim/login.jsp"); - } - - } - - /** - * Returns a short description of the servlet. - * - * @return a String containing servlet description - */ - @Override - public String getServletInfo() { - return "Short description"; - }// - -} diff --git a/src/main/java/uk/ac/dundee/computing/aec/instagrim/servlets/Register.java b/src/main/java/uk/ac/dundee/computing/aec/instagrim/servlets/Register.java deleted file mode 100644 index 20ae736a..00000000 --- a/src/main/java/uk/ac/dundee/computing/aec/instagrim/servlets/Register.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - -package uk.ac.dundee.computing.aec.instagrim.servlets; - -import com.datastax.driver.core.Cluster; -import java.io.IOException; -import java.io.PrintWriter; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import uk.ac.dundee.computing.aec.instagrim.lib.CassandraHosts; -import uk.ac.dundee.computing.aec.instagrim.models.User; - -/** - * - * @author Administrator - */ -@WebServlet(name = "Register", urlPatterns = {"/Register"}) -public class Register extends HttpServlet { - Cluster cluster=null; - public void init(ServletConfig config) throws ServletException { - // TODO Auto-generated method stub - cluster = CassandraHosts.getCluster(); - } - - - - - /** - * Handles the HTTP POST method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - String username=request.getParameter("username"); - String password=request.getParameter("password"); - - User us=new User(); - us.setCluster(cluster); - us.RegisterUser(username, password); - - response.sendRedirect("/Instagrim"); - - } - - /** - * Returns a short description of the servlet. - * - * @return a String containing servlet description - */ - @Override - public String getServletInfo() { - return "Short description"; - }// - -} diff --git a/src/main/java/uk/ac/dundee/computing/aec/instagrim/stores/LoggedIn.java b/src/main/java/uk/ac/dundee/computing/aec/instagrim/stores/LoggedIn.java deleted file mode 100644 index 8e1574e2..00000000 --- a/src/main/java/uk/ac/dundee/computing/aec/instagrim/stores/LoggedIn.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - -package uk.ac.dundee.computing.aec.instagrim.stores; - -/** - * - * @author Administrator - */ -public class LoggedIn { - boolean logedin=false; - String Username=null; - public void LogedIn(){ - - } - - public void setUsername(String name){ - this.Username=name; - } - public String getUsername(){ - return Username; - } - public void setLogedin(){ - logedin=true; - } - public void setLogedout(){ - logedin=false; - } - - public void setLoginState(boolean logedin){ - this.logedin=logedin; - } - public boolean getlogedin(){ - return logedin; - } -} diff --git a/src/main/java/uk/ac/dundee/computing/aec/instagrim/stores/Pic.java b/src/main/java/uk/ac/dundee/computing/aec/instagrim/stores/Pic.java deleted file mode 100644 index c36e3f6c..00000000 --- a/src/main/java/uk/ac/dundee/computing/aec/instagrim/stores/Pic.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package uk.ac.dundee.computing.aec.instagrim.stores; - -import com.datastax.driver.core.utils.Bytes; -import java.nio.ByteBuffer; - -/** - * - * @author Administrator - */ -public class Pic { - - private ByteBuffer bImage = null; - private int length; - private String type; - private java.util.UUID UUID=null; - - public void Pic() { - - } - public void setUUID(java.util.UUID UUID){ - this.UUID =UUID; - } - public String getSUUID(){ - return UUID.toString(); - } - public void setPic(ByteBuffer bImage, int length,String type) { - this.bImage = bImage; - this.length = length; - this.type=type; - } - - public ByteBuffer getBuffer() { - return bImage; - } - - public int getLength() { - return length; - } - - public String getType(){ - return type; - } - - public byte[] getBytes() { - - byte image[] = Bytes.getArray(bImage); - return image; - } - -} diff --git a/src/main/java/uk/ac/dundee/computing/aec/instagrin/filters/ProtectPages.java b/src/main/java/uk/ac/dundee/computing/aec/instagrin/filters/ProtectPages.java deleted file mode 100644 index 7e284d6c..00000000 --- a/src/main/java/uk/ac/dundee/computing/aec/instagrin/filters/ProtectPages.java +++ /dev/null @@ -1,248 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - -package uk.ac.dundee.computing.aec.instagrin.filters; - -import java.io.IOException; -import java.io.PrintStream; -import java.io.PrintWriter; -import java.io.StringWriter; -import javax.servlet.DispatcherType; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.annotation.WebFilter; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; -import uk.ac.dundee.computing.aec.instagrim.stores.LoggedIn; - -/** - * - * @author Administrator - */ -@WebFilter(filterName = "ProtectPages", urlPatterns = {"/upload.jsp"}, dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE}) -public class ProtectPages implements Filter { - - private static final boolean debug = true; - - // The filter configuration object we are associated with. If - // this value is null, this filter instance is not currently - // configured. - private FilterConfig filterConfig = null; - - public ProtectPages() { - } - - private void doBeforeProcessing(ServletRequest request, ServletResponse response) - throws IOException, ServletException { - if (debug) { - log("ProtectPages:DoBeforeProcessing"); - } - - // Write code here to process the request and/or response before - // the rest of the filter chain is invoked. - // For example, a logging filter might log items on the request object, - // such as the parameters. - /* - for (Enumeration en = request.getParameterNames(); en.hasMoreElements(); ) { - String name = (String)en.nextElement(); - String values[] = request.getParameterValues(name); - int n = values.length; - StringBuffer buf = new StringBuffer(); - buf.append(name); - buf.append("="); - for(int i=0; i < n; i++) { - buf.append(values[i]); - if (i < n-1) - buf.append(","); - } - log(buf.toString()); - } - */ - } - - private void doAfterProcessing(ServletRequest request, ServletResponse response) - throws IOException, ServletException { - if (debug) { - log("ProtectPages:DoAfterProcessing"); - } - - // Write code here to process the request and/or response after - // the rest of the filter chain is invoked. - // For example, a logging filter might log the attributes on the - // request object after the request has been processed. - /* - for (Enumeration en = request.getAttributeNames(); en.hasMoreElements(); ) { - String name = (String)en.nextElement(); - Object value = request.getAttribute(name); - log("attribute: " + name + "=" + value.toString()); - - } - */ - // For example, a filter might append something to the response. - /* - PrintWriter respOut = new PrintWriter(response.getWriter()); - respOut.println("

This has been appended by an intrusive filter."); - */ - } - - /** - * - * @param request The servlet request we are processing - * @param response The servlet response we are creating - * @param chain The filter chain we are processing - * - * @exception IOException if an input/output error occurs - * @exception ServletException if a servlet error occurs - */ - public void doFilter(ServletRequest request, ServletResponse response, - FilterChain chain) - throws IOException, ServletException { - - if (debug) { - log("ProtectPages:doFilter()"); - } - - doBeforeProcessing(request, response); - System.out.println("Doing filter"); - HttpServletRequest httpReq = (HttpServletRequest) request; - HttpSession session=httpReq.getSession(false); - LoggedIn li=(LoggedIn)session.getAttribute("LoggedIn"); - System.out.println("Session in filter "+session); - if ((li == null) || (li.getlogedin()==false)){ - System.out.println("Foward to login"); - RequestDispatcher rd=request.getRequestDispatcher("/login.jsp"); - rd.forward(request,response); - - - } - Throwable problem = null; - try { - chain.doFilter(request, response); - } catch (Throwable t) { - // If an exception is thrown somewhere down the filter chain, - // we still want to execute our after processing, and then - // rethrow the problem after that. - problem = t; - t.printStackTrace(); - } - - doAfterProcessing(request, response); - - // If there was a problem, we want to rethrow it if it is - // a known type, otherwise log it. - if (problem != null) { - if (problem instanceof ServletException) { - throw (ServletException) problem; - } - if (problem instanceof IOException) { - throw (IOException) problem; - } - sendProcessingError(problem, response); - } - } - - /** - * Return the filter configuration object for this filter. - */ - public FilterConfig getFilterConfig() { - return (this.filterConfig); - } - - /** - * Set the filter configuration object for this filter. - * - * @param filterConfig The filter configuration object - */ - public void setFilterConfig(FilterConfig filterConfig) { - this.filterConfig = filterConfig; - } - - /** - * Destroy method for this filter - */ - public void destroy() { - } - - /** - * Init method for this filter - */ - public void init(FilterConfig filterConfig) { - this.filterConfig = filterConfig; - if (filterConfig != null) { - if (debug) { - log("ProtectPages:Initializing filter"); - } - } - } - - /** - * Return a String representation of this object. - */ - @Override - public String toString() { - if (filterConfig == null) { - return ("ProtectPages()"); - } - StringBuffer sb = new StringBuffer("ProtectPages("); - sb.append(filterConfig); - sb.append(")"); - return (sb.toString()); - } - - private void sendProcessingError(Throwable t, ServletResponse response) { - String stackTrace = getStackTrace(t); - - if (stackTrace != null && !stackTrace.equals("")) { - try { - response.setContentType("text/html"); - PrintStream ps = new PrintStream(response.getOutputStream()); - PrintWriter pw = new PrintWriter(ps); - pw.print("\n\nError\n\n\n"); //NOI18N - - // PENDING! Localize this for next official release - pw.print("

The resource did not process correctly

\n
\n");                
-                pw.print(stackTrace);                
-                pw.print("
\n"); //NOI18N - pw.close(); - ps.close(); - response.getOutputStream().close(); - } catch (Exception ex) { - } - } else { - try { - PrintStream ps = new PrintStream(response.getOutputStream()); - t.printStackTrace(ps); - ps.close(); - response.getOutputStream().close(); - } catch (Exception ex) { - } - } - } - - public static String getStackTrace(Throwable t) { - String stackTrace = null; - try { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - t.printStackTrace(pw); - pw.close(); - sw.close(); - stackTrace = sw.getBuffer().toString(); - } catch (Exception ex) { - } - return stackTrace; - } - - public void log(String msg) { - filterConfig.getServletContext().log(msg); - } - -} diff --git a/src/main/webapp/META-INF/context.xml b/src/main/webapp/META-INF/context.xml index 5bc0dbd9..5bee3dc3 100644 --- a/src/main/webapp/META-INF/context.xml +++ b/src/main/webapp/META-INF/context.xml @@ -1,2 +1,2 @@ - + diff --git a/src/main/webapp/Styles.css b/src/main/webapp/Styles.css deleted file mode 100644 index a41cfe60..00000000 --- a/src/main/webapp/Styles.css +++ /dev/null @@ -1,16 +0,0 @@ -/* -To change this license header, choose License Headers in Project Properties. -To change this template file, choose Tools | Templates -and open the template in the editor. -*/ -/* - Created on : Sep 27, 2014, 1:34:36 PM - Author : Administrator -*/ - - -nav { - text-align: left; - border: 2px; - float: right; -} \ No newline at end of file diff --git a/src/main/webapp/UsersPics.jsp b/src/main/webapp/UsersPics.jsp deleted file mode 100644 index 6e11a143..00000000 --- a/src/main/webapp/UsersPics.jsp +++ /dev/null @@ -1,58 +0,0 @@ -<%-- - Document : UsersPics - Created on : Sep 24, 2014, 2:52:48 PM - Author : Administrator ---%> - -<%@page import="java.util.*"%> -<%@page contentType="text/html" pageEncoding="UTF-8"%> -<%@ page import="uk.ac.dundee.computing.aec.instagrim.stores.*" %> - - - - - Instagrim - - - -
- -

InstaGrim !

-

Your world in Black and White

-
- - - -
-

Your Pics

- <% - java.util.LinkedList lsPics = (java.util.LinkedList) request.getAttribute("Pics"); - if (lsPics == null) { - %> -

No Pictures found

- <% - } else { - Iterator iterator; - iterator = lsPics.iterator(); - while (iterator.hasNext()) { - Pic p = (Pic) iterator.next(); - - %> -
<% - - } - } - %> -
- - - diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index f2e8ef52..23a7b5b6 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -1,54 +1,17 @@ <%-- Document : index - Created on : Sep 28, 2014, 7:01:44 PM - Author : Administrator + Created on : 24-Nov-2014, 15:09:25 + Author : Shaun Smith --%> - <%@page contentType="text/html" pageEncoding="UTF-8"%> -<%@page import="uk.ac.dundee.computing.aec.instagrim.stores.*" %> - Instagrim - + JSP Page -
-

InstaGrim !

-

Your world in Black and White

-
- -
-
    - -
  • © Andy C
  • -
-
+

Hello World!

diff --git a/src/main/webapp/login.jsp b/src/main/webapp/login.jsp deleted file mode 100644 index 95f92a02..00000000 --- a/src/main/webapp/login.jsp +++ /dev/null @@ -1,46 +0,0 @@ -<%-- - Document : login.jsp - Created on : Sep 28, 2014, 12:04:14 PM - Author : Administrator ---%> - -<%@page contentType="text/html" pageEncoding="UTF-8"%> - - - - - Instagrim - - - - -
-

InstaGrim !

-

Your world in Black and White

-
- - -
-

Login

-
-
    -
  • User Name
  • -
  • Password
  • -
-
- -
- -
- - - diff --git a/src/main/webapp/register.jsp b/src/main/webapp/register.jsp deleted file mode 100644 index 8acaadac..00000000 --- a/src/main/webapp/register.jsp +++ /dev/null @@ -1,45 +0,0 @@ -<%-- - Document : register.jsp - Created on : Sep 28, 2014, 6:29:51 PM - Author : Administrator ---%> - -<%@page contentType="text/html" pageEncoding="UTF-8"%> - - - - - Instagrim - - - -
-

InstaGrim !

-

Your world in Black and White

-
- - -
-

Register as user

-
-
    -
  • User Name
  • -
  • Password
  • -
-
- -
- -
- - - diff --git a/src/main/webapp/test.jsp b/src/main/webapp/test.jsp deleted file mode 100644 index 985ba8af..00000000 --- a/src/main/webapp/test.jsp +++ /dev/null @@ -1,22 +0,0 @@ -<%-- - Document : test - Created on : Sep 29, 2014, 9:16:48 AM - Author : Administrator ---%> - -<%@page contentType="text/html" pageEncoding="UTF-8"%> -<%@ page import="uk.ac.dundee.computing.aec.instagrim.stores.*" %> - - - - - JSP Page - - -

Hello World!

- <% - Pic ps = new Pic(); - LoggedIn lg = new LoggedIn(); - %> - - diff --git a/src/main/webapp/upload.jsp b/src/main/webapp/upload.jsp deleted file mode 100644 index 1e77ad41..00000000 --- a/src/main/webapp/upload.jsp +++ /dev/null @@ -1,41 +0,0 @@ -<%-- - Document : upload - Created on : Sep 22, 2014, 6:31:50 PM - Author : Administrator ---%> - -<%@page contentType="text/html" pageEncoding="UTF-8"%> - - - - - Instagrim - - - -

InstaGrim !

-

Your world in Black and White

- - -
-

File Upload

-
- File to upload:
- -
- to upload the file! -
- -
- - -