From 97b7a1e491b419a82bb94289d1f957be48bcf35e Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 28 Jul 2008 15:00:27 +0000 Subject: Added well-formed checks After recieving an Item, InstaDisc Client now checks the item for a status of being Well-Formed, as in, it has all of the required headers, the category exists, the category's required semantics are present and the verification is valid. --- .../fourisland/instadisc/CloseObjectThread.java | 33 +++++++++++++ .../com/fourisland/instadisc/InstaDiscThread.java | 3 +- .../src/com/fourisland/instadisc/Item/Item.java | 4 ++ .../src/com/fourisland/instadisc/Item/MD5.java | 57 ++++++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 client/trunk/src/com/fourisland/instadisc/CloseObjectThread.java create mode 100644 client/trunk/src/com/fourisland/instadisc/Item/MD5.java (limited to 'client') diff --git a/client/trunk/src/com/fourisland/instadisc/CloseObjectThread.java b/client/trunk/src/com/fourisland/instadisc/CloseObjectThread.java new file mode 100644 index 0000000..af8cecc --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/CloseObjectThread.java @@ -0,0 +1,33 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.instadisc; + +import java.io.IOException; +import java.net.ServerSocket; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author hatkirby + */ +class CloseObjectThread implements Runnable{ + + ServerSocket svr; + + public CloseObjectThread(ServerSocket svr) { + this.svr = svr; + } + + public void run() { + try { + svr.close(); + } catch (IOException ex) { + Logger.getLogger(CloseObjectThread.class.getName()).log(Level.SEVERE, null, ex); + } + } + +} diff --git a/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java b/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java index 69a8e49..fcde8d8 100644 --- a/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java +++ b/client/trunk/src/com/fourisland/instadisc/InstaDiscThread.java @@ -30,6 +30,7 @@ public class InstaDiscThread implements Runnable { ServerSocket svr = new ServerSocket(); java.net.InetSocketAddress addr = new java.net.InetSocketAddress(4444); svr.bind(addr); + Runtime.getRuntime().addShutdownHook(new Thread(new CloseObjectThread(svr))); while (!cancelled) { try { Socket s = svr.accept(); @@ -90,7 +91,7 @@ class HandleItemThread implements Runnable { try { String[] nameVal = headers[i].split(": "); String name = nameVal[0]; - String value = nameVal[1]; + String value = nameVal[1].trim(); headerMap.put(name, value); } catch (Exception ex) { break; diff --git a/client/trunk/src/com/fourisland/instadisc/Item/Item.java b/client/trunk/src/com/fourisland/instadisc/Item/Item.java index f13ec3d..fd52d5f 100644 --- a/client/trunk/src/com/fourisland/instadisc/Item/Item.java +++ b/client/trunk/src/com/fourisland/instadisc/Item/Item.java @@ -23,5 +23,9 @@ public class Item { public void start() { WellFormedItem wfi = new WellFormedItem(this); + if (wfi.check()) + { + + } } } diff --git a/client/trunk/src/com/fourisland/instadisc/Item/MD5.java b/client/trunk/src/com/fourisland/instadisc/Item/MD5.java new file mode 100644 index 0000000..9c4d5aa --- /dev/null +++ b/client/trunk/src/com/fourisland/instadisc/Item/MD5.java @@ -0,0 +1,57 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.instadisc.Item; + +import java.security.MessageDigest; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author hatkirby + */ +public class MD5 { + + String ver; + + public MD5(String ver) { + this.ver = ver; + } + + public String hash() + { + StringBuilder verify = new StringBuilder(); + try { + MessageDigest md5 = MessageDigest.getInstance("MD5"); + int i = 0; + byte[] create = new byte[ver.length()]; + for (i = 0; i < ver.length(); i++) { + create[i] = (byte) ver.charAt(i); + } + byte buffer[] = md5.digest(create); + for (i = 0; i < buffer.length; i++) { + String hex = Integer.toHexString(buffer[i]); + verify.append(pad(hex.substring(max(hex.length() - 2, 0)),"0",2)); + } + } catch (Exception ex) { + Logger.getLogger(WellFormedItem.class.getName()).log(Level.SEVERE, null, ex); + } + return verify.toString(); + } + + private int max(int x, int y) + { + return (x > y ? x : y); + } + + private String pad(String in, String pad, int len) + { + while (in.length() < len) + { + in = pad + in; + } + return in; + } +} -- cgit 1.4.1