package crushftp.server;
import java.io.*;
import java.util.*;
import java.net.*;
import java.text.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Attribute;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import crushftp.handlers.Common;
public class ServerSessionNotMacXML
{
static RandomAccessFile log = null;
SAXBuilder sax = new SAXBuilder();
String home = "/Library/Application Support/NotMac/syncxml/";
static Properties tokens = new Properties();
static Properties locktokens = new Properties();
ServerSessionHTTP httpSession = null;
SimpleDateFormat sdf_createDate = new SimpleDateFormat("yyyy MM dd, HH:mm:ss z",Locale.US);
String fileData = "";
String httpVersion = "1.1";
public void process(Vector headers, ServerSessionHTTP httpSession) throws Exception
{
if (log == null)
{
try{log = new RandomAccessFile("/Library/Application Support/NotMac/sync.log","rw");log.seek(log.length());}catch(Exception e){e.printStackTrace();};
}
this.httpSession = httpSession;
long http_len_max = 0;
for (int x=0; x<headers.size(); x++)
{
String data = headers.elementAt(x).toString();
if (x == 0 && data.endsWith("/1.0")) httpVersion = "1.0";
if (data.toUpperCase().startsWith("CONTENT-LENGTH: "))
{
try{http_len_max = Long.parseLong(data.substring(data.toUpperCase().indexOf("CONTENT-LENGTH:") + "CONTENT-LENGTH:".length()).trim());}catch(Exception e){}
}
}
XMLOutputter xmlOut = new XMLOutputter();
Format f = Format.getPrettyFormat();
f.setExpandEmptyElements(false);
//f.setIndent("\t");
xmlOut.setFormat(f);
String xml = httpSession.get_raw_http_command((int)http_len_max);
if (xml.indexOf("type=\"putfile\"") >= 0)
{
fileData = xml.substring(xml.indexOf("file fiop="));
String fileLength = fileData.substring(fileData.indexOf("length=\"")+"length=\"".length(),fileData.indexOf("\"",fileData.indexOf("length=\"")+"length=\"".length()));
fileData = fileData.substring(fileData.indexOf("/>")+2);
fileData = fileData.substring(0,Integer.parseInt(fileLength));
xml = xml.substring(0, xml.indexOf(">",xml.indexOf("file fiop="))+1)+xml.substring(xml.indexOf(">",xml.indexOf("file fiop="))+1+Integer.parseInt(fileLength)+"</file>".length());
}
Document doc2 = sax.build(new StringReader(xml));
log.write(("REQUEST:"+xmlOut.outputString(doc2)+"\r\n\r\n").getBytes());
Element response = processXML(xml);
Document doc = new Document(response);
xml = xmlOut.outputString(doc);
if (xml.indexOf("type=\"getfile\"") >= 0)
{
xml = Common.replace_str(xml,"___fileData___",fileData);
fileData = "";
}
xml = xml.substring(xml.indexOf("?>")+2);
log.write(("RESPONSE:"+xml+"\r\n\r\n").getBytes());
httpSession.write_command_http("HTTP/"+httpVersion+" 200 OK");
if (httpVersion.equals("1.0"))
{
httpSession.write_standard_headers_1_0();
httpSession.write_command_http("Content-Type: text/xml");
httpSession.done = true;
}
else
{
httpSession.write_standard_headers();
httpSession.write_command_http("Content-Type: text/html");
httpSession.write_command_http("Content-Length: "+xml.getBytes().length);
}
httpSession.write_command_http("");
//must do a direct write as encoding to UTF8 breaks the response when file data is embedded
httpSession.original_os.write(xml.getBytes());
httpSession.original_os.flush();
}
public Element processXML(String xml) throws Exception
{
Element response = new Element("response");
response.setAttribute("version","1.0");
response.setAttribute("id","");
Document doc = sax.build(new StringReader(xml));
Element request = doc.getRootElement();
try{response.setAttribute("id",request.getAttributeValue("id"));}catch(Exception e){}
List transactions = request.getChildren("transaction");
Properties credentials = null;
StringBuffer locktoken = new StringBuffer();
for (int x=0; x<transactions.size(); x++)
{
Element transaction = (Element)transactions.get(x);
try
{
if (transaction.getAttributeValue("type").equals("authorization"))
{
credentials = processAuthorization(transaction, response, locktoken);
}
else if (transaction.getAttributeValue("type").equals("select"))
{
processSelect(transaction, response, credentials);
}
else if (transaction.getAttributeValue("type").equals("delete"))
{
processDelete(transaction, response, credentials);
}
else if (transaction.getAttributeValue("type").equals("create"))
{
processCreate(transaction, response, credentials);
}
else if (transaction.getAttributeValue("type").equals("commit"))
{
processCommit(transaction, response, credentials, locktoken.toString());
}
else if (transaction.getAttributeValue("type").equals("reset"))
{
processReset(transaction, response, credentials, locktoken.toString());
}
else if (transaction.getAttributeValue("type").equals("update"))
{
processUpdate(transaction, response, credentials);
}
else if (transaction.getAttributeValue("type").equals("lockacquire"))
{
processLock(transaction, response, credentials,"acquire");
}
else if (transaction.getAttributeValue("type").equals("lockrenew"))
{
processLock(transaction, response, credentials,"renew");
}
else if (transaction.getAttributeValue("type").equals("lockrelease"))
{
processLock(transaction, response, credentials,"release");
}
else if (transaction.getAttributeValue("type").equals("putfile"))
{
processPutfile(transaction, response, credentials, locktoken.toString());
}
else if (transaction.getAttributeValue("type").equals("getfile"))
{
processGetfile(transaction, response, credentials, locktoken.toString());
}
else //return a success response
{
Element transaction2 = new Element("transaction");
response.addContent(transaction2);
transaction2.setAttribute("type",transaction.getAttributeValue("type"));
transaction2.setAttribute("version","1.0");
transaction2.setAttribute("id",transaction.getAttributeValue("id"));
addSuccess(transaction2);
}
}
catch(Exception e)
{
e.printStackTrace();
Element transaction2 = response.getChild("transaction");
if (transaction2 == null)
{
transaction2 = new Element("transaction");
response.addContent(transaction2);
}
addFailure(transaction2,"-1","-1 An error occured. "+e.getMessage());
transaction2.setAttribute("type",transaction.getAttributeValue("type"));
transaction2.setAttribute("version","1.0");
transaction2.setAttribute("id",transaction.getAttributeValue("id"));
}
}
return response;
}
public Properties processAuthorization(Element t, Element response, StringBuffer locktoken) throws Exception
{
Element transaction = new Element("transaction");
response.addContent(transaction);
transaction.setAttribute("type","authorization");
transaction.setAttribute("version","1.0");
transaction.setAttribute("id",t.getAttributeValue("id"));
Properties p = new Properties();
Element object = t.getChild("object");
getExtendedAttributes(object,p);
if (!p.getProperty("locktoken","").equals("")) locktoken.append(p.getProperty("locktoken"));
boolean ok = true;
//TODO - authenticate user
if (p.getProperty("newtoken","").equals("1"))
{
//do something with p now that we have the data.
Element user = getCredentialsObject(p,true);
transaction.addContent(user);
Properties token = new Properties();
getExtendedAttributes(user,token);
tokens.put(token.getProperty("token"),p);
p.put("resourceguid",user.getAttributeValue("resourceguid"));
}
else
{
//validate the token they sent to see if its still valid
if (!p.getProperty("token","").equals(""))
{
p = (Properties)tokens.get(p.getProperty("token"));
transaction.addContent(getCredentialsObject(p,false));
}
}
if (ok) addSuccess(transaction);
else
{
addFailure(transaction,"-1","-1 Invalid username or password.");
p.put("authenticated","false");
}
return p;
}
public void processSelect(Element t, Element response, Properties credentials) throws Exception
{
Element transaction = new Element("transaction");
response.addContent(transaction);
transaction.setAttribute("type","select");
transaction.setAttribute("version","1.0");
transaction.setAttribute("id",t.getAttributeValue("id"));
Properties p = new Properties();
Element object = t.getChild("object");
getExtendedAttributes(object,p);
int storageconsumed = 0;
if ((p.getProperty("info","").equals("device") || p.getProperty("info","").equals("devices")) && object.getAttributeValue("entityname").equals(".mac"))
{
//list known computers registered to this .mac credential object
Element user = getCredentialsObject(credentials, false);
transaction.addContent(user);
File list[] = new File(home+"users/"+credentials.getProperty("username")+"/device/").listFiles();
if (list != null)
{
for (int x=0; x<list.length; x++)
{
if (list[x].isDirectory())
{
Element object2 = new Element("object");
user.addContent(object2);
loadObject(object2,list[x].getCanonicalPath()+"/");
}
}
}
}
else if (p.getProperty("info","").equals("dc"))
{
Element machine = new Element("object");
transaction.addContent(machine);
loadObject(machine,home+"users/"+credentials.getProperty("username")+"/device/"+object.getAttributeValue("resourceguid")+"/");
machine.removeChildren("attribute");
File list[] = new File(home+"users/"+credentials.getProperty("username")+"/dc/").listFiles();
if (list != null)
{
for (int x=0; x<list.length; x++)
{
if (list[x].isDirectory())
{
Element object2 = new Element("object");
machine.addContent(object2);
Properties raw = loadObject(object2,list[x].getCanonicalPath()+"/");
Properties infoMachine = getAttributes(list[x].getCanonicalPath()+"/"+object.getAttributeValue("resourceguid")+"/");
if (infoMachine != null && infoMachine.containsKey("localversion"))
{
addAtribute(object2,"localversion",infoMachine.getProperty("localversion","0"),false);
}
storageconsumed += Integer.parseInt(((Properties)raw.get("attributes")).getProperty("storageconsumed","0"));
}
}
}
addAtribute(machine,"storageconsumed",storageconsumed+"",false);
}
addSuccess(transaction);
}
public void processUpdate(Element t, Element response, Properties credentials) throws Exception
{
Element transaction = new Element("transaction");
response.addContent(transaction);
transaction.setAttribute("type","update");
transaction.setAttribute("version","1.0");
transaction.setAttribute("id",t.getAttributeValue("id"));
Properties p = new Properties();
Element object = t.getChild("object");
getExtendedAttributes(object,p);
boolean ok = true;
Element object2 = new Element("object");
try{loadObject(object2,home+"users/"+credentials.getProperty("username")+"/device/"+object.getAttributeValue("resourceguid")+"/");}catch(Exception e){ok = false;}
Properties attributes = new Properties();
getExtendedAttributes(object2,attributes);
Enumeration keys = p.keys();
while (keys.hasMoreElements())
{
String key = keys.nextElement().toString();
String val = p.getProperty(key);
String val2 = attributes.getProperty(key,"");
if (!val.equals(val2)) ok = false;
}
if (p.size() > 0 && ok)
{
//never had an example of it actually doing an update
}
else //no matches
{
ok = false;
}
if (ok) addSuccess(transaction);
else addFailure(transaction,"47","47 Device named was not found in users PIN (Could not find specified device.)");
}
public void processLock(Element t, Element response, Properties credentials, String type) throws Exception
{
Element transaction = new Element("transaction");
response.addContent(transaction);
transaction.setAttribute("type","lock"+type);
transaction.setAttribute("version","1.0");
transaction.setAttribute("id",t.getAttributeValue("id"));
Properties p = new Properties();
Element object = t.getChild("object");
getExtendedAttributes(object,p);
Element object2 = new Element("object");
loadObject(object2,home+"users/"+credentials.getProperty("username")+"/device/"+object.getAttributeValue("resourceguid")+"/");
Properties attributes = new Properties();
getExtendedAttributes(object2,attributes);
object2.removeChildren("attribute");
transaction.addContent(object2);
if (type.equals("acquire"))
{
//p.getProperty("type").equals("write");
//p.getProperty("duration");
addAtribute(object2,"sessionkey",makeGuid("",40),false);
String guid = makeGuid("",40);
addAtribute(object2,"locktoken",guid,false);
locktokens.put(guid,object.getAttributeValue("resourceguid"));
}
else if (type.equals("renew"))
{
//do nothing...just renew it
}
else if (type.equals("release"))
{
object2.removeAttribute("resourceid");
object2.removeAttribute("entitytype");
//do nothing...just release it
}
addSuccess(transaction);
}
public void processDelete(Element t, Element response, Properties credentials) throws Exception
{
Element transaction = new Element("transaction");
response.addContent(transaction);
transaction.setAttribute("type","delete");
transaction.setAttribute("version","1.0");
transaction.setAttribute("id",t.getAttributeValue("id"));
Properties p = new Properties();
Element object = t.getChild("object");
getExtendedAttributes(object,p);
Element object3 = new Element("object");
loadObject(object3,home+"users/"+credentials.getProperty("username")+"/device/"+object.getAttributeValue("resourceguid")+"/");
Properties entittyname = new Properties();
getExtendedAttributes(object3,entittyname);
object3.removeChildren("attribute");
object3.removeAttribute("resourceid");
object3.removeAttribute("entitytype");
transaction.addContent(object3);
Common.recurseDelete(home+"users/"+credentials.getProperty("username")+"/device/"+object.getAttributeValue("resourceguid")+"/",false);
addSuccess(transaction);
}
public void processPutfile(Element t, Element response, Properties credentials, String locktoken) throws Exception
{
Element transaction = new Element("transaction");
response.addContent(transaction);
transaction.setAttribute("type","putfile");
transaction.setAttribute("version","1.0");
transaction.setAttribute("id",t.getAttributeValue("id"));
Properties p = new Properties();
Element object = t.getChild("object");
getExtendedAttributes(object,p);
String machineGuid = locktokens.getProperty(locktoken);
Element file = object.getChild("file");
String fiop[] = file.getAttributeValue("fiop").replace('|',',').split(",");
new File(home+"users/"+credentials.getProperty("username")+"/dc/"+object.getAttributeValue("resourceguid")+"/versions/").mkdirs();
String filename = fiop[2];
filename = filename.replace('/',' ');
filename = filename.replace('\\',' ');
RandomAccessFile out = new RandomAccessFile(home+"users/"+credentials.getProperty("username")+"/dc/"+object.getAttributeValue("resourceguid")+"/versions/"+filename,"rw");
out.write(fileData.getBytes());
out.close();
Element object3 = new Element("object");
Properties raw = loadObject(object3,home+"users/"+credentials.getProperty("username")+"/dc/"+object.getAttributeValue("resourceguid")+"/");
object3.removeChildren("attribute");
object3.removeAttribute("resourceid");
object3.removeAttribute("entitytype");
transaction.addContent(object3);
Properties info = (Properties)raw.get("info");
Properties attributes = (Properties)raw.get("attributes");
attributes.put("storageconsumed",fileData.length()+"");
attributes.put("version",fiop[1]);
attributes.put("f1version",fiop[1]);
attributes.remove("localversion");
Common common_code = new Common();
common_code.writeXMLObject(home+"users/"+credentials.getProperty("username")+"/dc/"+object.getAttributeValue("resourceguid")+"/info.XML",info,"props");
common_code.writeXMLObject(home+"users/"+credentials.getProperty("username")+"/dc/"+object.getAttributeValue("resourceguid")+"/attributes.XML",attributes,"props");
Properties localInfo = new Properties();
Properties localAttributes = getAttributes(home+"users/"+credentials.getProperty("username")+"/dc/"+object.getAttributeValue("resourceguid")+"/"+machineGuid+"/");
if (localAttributes == null) localAttributes = new Properties();
localAttributes.put("localversion",fiop[1]);
new File(home+"users/"+credentials.getProperty("username")+"/dc/"+object.getAttributeValue("resourceguid")+"/"+machineGuid+"/").mkdirs();
common_code.writeXMLObject(home+"users/"+credentials.getProperty("username")+"/dc/"+object.getAttributeValue("resourceguid")+"/"+machineGuid+"/info.XML",localInfo,"props");
common_code.writeXMLObject(home+"users/"+credentials.getProperty("username")+"/dc/"+object.getAttributeValue("resourceguid")+"/"+machineGuid+"/attributes.XML",localAttributes,"props");
fileData = "";
updateMachineSyncDates(credentials, machineGuid);
addSuccess(transaction);
}
public void processGetfile(Element t, Element response, Properties credentials, String locktoken) throws Exception
{
Element transaction = new Element("transaction");
response.addContent(transaction);
transaction.setAttribute("type","getfile");
transaction.setAttribute("version","1.0");
transaction.setAttribute("id",t.getAttributeValue("id"));
Properties p = new Properties();
Element object = t.getChild("object");
getExtendedAttributes(object,p);
String machineGuid = locktokens.getProperty(locktoken);
Element file = object.getChild("file");
String fiop[] = file.getAttributeValue("fiop").replace('|',',').split(",");
String filename = fiop[2];
filename = filename.replace('/',' ');
filename = filename.replace('\\',' ');
RandomAccessFile in = new RandomAccessFile(home+"users/"+credentials.getProperty("username")+"/dc/"+object.getAttributeValue("resourceguid")+"/versions/"+filename,"r");
byte b[] = new byte[(int)in.length()];
in.readFully(b);
in.close();
fileData = new String(b);
Element object3 = new Element("object");
object3.removeChildren("attribute");
object3.removeAttribute("resourceid");
object3.removeAttribute("entitytype");
transaction.addContent(object3);
Element file2 = new Element("file");
object3.addContent(file2);
file2.setAttribute("length",fileData.length()+"");
file2.setText("___fileData___");
updateMachineSyncDates(credentials, machineGuid);
addSuccess(transaction);
}
public void processCreate(Element t, Element response, Properties credentials) throws Exception
{
Element transaction = new Element("transaction");
response.addContent(transaction);
transaction.setAttribute("type","create");
transaction.setAttribute("version","1.0");
transaction.setAttribute("id",t.getAttributeValue("id"));
Properties p = new Properties();
Element object = t.getChild("object");
getExtendedAttributes(object,p);
String guid = makeGuid("",40);
String resourceid = new Date().getTime()+"";
resourceid = resourceid.substring(resourceid.length()-7);
new File(home+"users/"+credentials.getProperty("username")+"/device/"+guid).mkdirs();
Properties info = new Properties();
info.put("entityid",object.getAttributeValue("entityid"));
Properties attributes = new Properties();
attributes.put("entityid",object.getAttributeValue("entityid"));
Enumeration keys = p.keys();
while (keys.hasMoreElements())
{
String key = keys.nextElement().toString();
String val = p.getProperty(key);
attributes.put(key,val);
}
if (object.getAttributeValue("entityid").equals("11"))
{
info.put("entitytype","1");
info.put("entityname","macintosh");
info.put("resourceid",resourceid);
info.put("resourceguid",guid);
attributes.put("resourceid",resourceid);
attributes.put("resourceguid",guid);
attributes.put("parentid","0");
attributes.put("inservice","0");
attributes.put("status","0");
attributes.put("umi","0");
attributes.put("flag_autotrigger","0");
}
Common common_code = new Common();
common_code.writeXMLObject(home+"users/"+credentials.getProperty("username")+"/device/"+guid+"/info.XML",info,"props");
common_code.writeXMLObject(home+"users/"+credentials.getProperty("username")+"/device/"+guid+"/attributes.XML",attributes,"props");
Element object2 = new Element("object");
transaction.addContent(object2);
loadObject(object2,home+"users/"+credentials.getProperty("username")+"/device/"+guid+"/");
attributes.put("createdate", sdf_createDate.format(new Date()));
common_code.writeXMLObject(home+"users/"+credentials.getProperty("username")+"/device/"+guid+"/attributes.XML",attributes,"props");
addSuccess(transaction);
}
public void processCommit(Element t, Element response, Properties credentials, String locktoken) throws Exception
{
Element transaction = new Element("transaction");
response.addContent(transaction);
transaction.setAttribute("type","commit");
transaction.setAttribute("version","1.0");
transaction.setAttribute("id",t.getAttributeValue("id"));
Properties p = new Properties();
Properties p2 = new Properties();
Element object = t.getChild("object");
getExtendedAttributes(object,p);
getNormalAttributes(object,p2);
String guid = object.getAttributeValue("resourceguid");
String entityname = object.getAttributeValue("entityname");
if (!new File(home+"users/"+credentials.getProperty("username")+"/dc/"+guid+"/").exists())
{
makeObject(home+"users/"+credentials.getProperty("username")+"/dc/",null,null,entityname,null, guid);
}
Element object2 = new Element("object");
transaction.addContent(object2);
Properties raw = loadObject(object2,home+"users/"+credentials.getProperty("username")+"/dc/"+guid+"/");
Properties info = (Properties)raw.get("info");
info.putAll(p2);
Properties attributes = (Properties)raw.get("attributes");
//special case where "adds" is always added and not just replaced.
p.put("adds",(Integer.parseInt(p.getProperty("adds","0")) + Integer.parseInt(attributes.getProperty("adds","0")))+"");
attributes.putAll(p);
String localVersion = null;
if (attributes.containsKey("localversion"))
{
localVersion = attributes.getProperty("localversion");
attributes.remove("localversion");
}
Common common_code = new Common();
common_code.writeXMLObject(home+"users/"+credentials.getProperty("username")+"/dc/"+guid+"/info.XML",info,"props");
common_code.writeXMLObject(home+"users/"+credentials.getProperty("username")+"/dc/"+guid+"/attributes.XML",attributes,"props");
if (localVersion != null)
{
String machineGuid = locktokens.getProperty(locktoken);
new File(home+"users/"+credentials.getProperty("username")+"/dc/"+object.getAttributeValue("resourceguid")+"/"+machineGuid+"/").mkdirs();
Properties localAttributes = getAttributes(home+"users/"+credentials.getProperty("username")+"/dc/"+guid+"/"+machineGuid+"/");
if (localAttributes == null) localAttributes = new Properties();
localAttributes.put("localversion",localVersion);
common_code.writeXMLObject(home+"users/"+credentials.getProperty("username")+"/dc/"+object.getAttributeValue("resourceguid")+"/"+machineGuid+"/attributes.XML",localAttributes,"props");
}
loadObject(object2,home+"users/"+credentials.getProperty("username")+"/dc/"+guid+"/");
object2.removeChildren("attribute");
addSuccess(transaction);
}
public void processReset(Element t, Element response, Properties credentials, String locktoken) throws Exception
{
Element transaction = new Element("transaction");
response.addContent(transaction);
transaction.setAttribute("type","commit");
transaction.setAttribute("version","1.0");
transaction.setAttribute("id",t.getAttributeValue("id"));
Properties p = new Properties();
Properties p2 = new Properties();
Element object = t.getChild("object");
getExtendedAttributes(object,p);
getNormalAttributes(object,p2);
String guid = object.getAttributeValue("resourceguid");
Properties raw = loadObject(null,home+"users/"+credentials.getProperty("username")+"/dc/"+guid+"/");
Properties info = (Properties)raw.get("info");
Properties attributes = (Properties)raw.get("attributes");
attributes.put("f1version","-2");
attributes.put("version","-2");
attributes.put("baseversion","-2");
attributes.put("storageconsumed","0");
attributes.put("adds","0");
attributes.put("updates","0");
attributes.put("deletes","0");
attributes.put("moves","0");
attributes.put("renames","0");
attributes.put("lastmoddate",httpSession.sdf_rfc1123.format(new Date()));
Common.recurseDelete(home+"users/"+credentials.getProperty("username")+"/dc/"+guid+"/",false);
new File(home+"users/"+credentials.getProperty("username")+"/dc/"+guid+"/").mkdirs();
Common common_code = new Common();
common_code.writeXMLObject(home+"users/"+credentials.getProperty("username")+"/dc/"+guid+"/info.XML",info,"props");
common_code.writeXMLObject(home+"users/"+credentials.getProperty("username")+"/dc/"+guid+"/attributes.XML",attributes,"props");
Element object2 = new Element("object");
transaction.addContent(object2);
loadObject(object2,home+"users/"+credentials.getProperty("username")+"/dc/"+guid+"/");
object2.removeChildren("attribute");
addSuccess(transaction);
}
public void makeObject(String path, String entityid, String entitytype, String entityname, String description, String guid) throws Exception
{
if (guid == null) guid = makeGuid("",40);
new File(path+guid).mkdirs();
String resourceid = new Date().getTime()+"";
resourceid = resourceid.substring(resourceid.length()-7);
new File(path+guid).mkdir();
Properties info = new Properties();
info.put("resourceguid",guid);
if (entityid != null) info.put("entityid",entityid);
if (entitytype != null) info.put("entitytype",entitytype);
info.put("entityname",entityname);
info.put("resourceid",resourceid);
Properties attributes = new Properties();
attributes.put("resourceguid",guid);
if (entityid != null) attributes.put("entityid",entityid);
attributes.put("resourceid",resourceid);
attributes.put("f1version","-2");
attributes.put("version","-2");
attributes.put("baseversion","-2");
attributes.put("storageconsumed","0");
attributes.put("name",entityname);
attributes.put("adds","0");
attributes.put("updates","0");
attributes.put("deletes","0");
attributes.put("moves","0");
attributes.put("renames","0");
if (description != null) attributes.put("description",description);
attributes.put("lastmoddate",httpSession.sdf_rfc1123.format(new Date()));
if (entityname.equals("ICAL"))
{
info.put("entityid","1");
info.put("entitytype","10");
attributes.remove("resourceid");
attributes.put("description","Calendar");
attributes.put("f1version","0");
attributes.put("version","0");
attributes.put("baseversion","0");
}
if (entityname.equals("BKMK"))
{
info.put("entityid","6");
info.put("entitytype","10");
attributes.remove("resourceid");
attributes.put("description","Bookmarks");
attributes.put("f1version","0");
attributes.put("version","0");
attributes.put("baseversion","0");
}
if (entityname.equals("CONT"))
{
info.put("entityid","2");
info.put("entitytype","10");
attributes.remove("resourceid");
attributes.put("description","Contacts");
attributes.put("f1version","0");
attributes.put("version","0");
attributes.put("baseversion","0");
}
Common common_code = new Common();
common_code.writeXMLObject(path+guid+"/info.XML",info,"props");
common_code.writeXMLObject(path+guid+"/attributes.XML",attributes,"props");
}
static public String makeGuid(String moreChars, int maxLen) {
String chars = "1234567890ABCDEF"+moreChars;
String rand = "";
for (int i = 0; i < maxLen; i++) rand += chars.charAt((int) (Math.random() * (chars.length() - 1)));
return rand;
}
public Element getUser()
{
Element o = new Element("object");
o.setAttribute("entityid","11");
o.setAttribute("entitytype","1");
o.setAttribute("entityname","macintosh");
o.setAttribute("resourceid","5449562");
o.setAttribute("resourceguid","E624678D-0114-1000-88B8-000A95E4A106");
return o;
}
public void addSuccess(Element e)
{
Element result = new Element("result");
e.addContent(result);
addAtribute(result,"resultcode","0",false);
addAtribute(result,"resulttext","success",false);
}
public void addFailure(Element e, String errorCode, String errorMsg)
{
Element result = new Element("result");
e.addContent(result);
addAtribute(result,"resultcode",errorCode,false);
addAtribute(result,"resulttext",errorMsg,false);
addAtribute(result,"hostname","server",false);
addAtribute(result,"timestamp",sdf_createDate.format(new Date()),false);
}
public Element getCredentialsObject(Properties p, boolean newToken) throws Exception
{
if (p == null || p.getProperty("username") == null) throw new Exception("User session expired, or invalid.");
String username = p.getProperty("username");
String password = p.getProperty("password");
boolean ok = httpSession.thisSession.verify_user(username.trim(),password.trim());
if (!ok) throw new Exception("Login Invalid.");
Element object = new Element("object");
Properties prop = (Properties)Common.readXMLObject(home+"users/"+username+"/info.XML");
copyKeysToAttributes(prop,object,true);
if (newToken)
{
addAtribute(object,"token",new Date().getTime()+"",false);
}
return object;
}
public void addAtribute(Element item, String name, String value, boolean inline)
{
if (inline) item.setAttribute(name,value);
else
{
Element e = new Element("attribute");
e.setAttribute("name",name);
e.setAttribute("value",value);
item.addContent(e);
}
}
public void copyKeysToAttributes(Properties prop, Element object, boolean inline)
{
Enumeration keys = prop.keys();
while(keys.hasMoreElements())
{
String key = keys.nextElement().toString();
addAtribute(object,key,prop.getProperty(key),inline);
}
}
public void getExtendedAttributes(Element object, Properties p)
{
List attributes = object.getChildren("attribute");
for (int x=0; x<attributes.size(); x++)
{
Element attribute = (Element)attributes.get(x);
p.put(attribute.getAttributeValue("name"), attribute.getAttributeValue("value"));
}
}
public void getNormalAttributes(Element object, Properties p)
{
List attributes = object.getAttributes();
for (int x=0; x<attributes.size(); x++)
{
Attribute attribute = (Attribute)attributes.get(x);
p.put(attribute.getName(), attribute.getValue());
}
}
public Properties loadObject(Element object, String path)
{
Properties raw = new Properties();
Properties info = (Properties)Common.readXMLObject(path+"info.XML");
if (object != null) copyKeysToAttributes(info,object,true);
Properties attributes = getAttributes(path);
if (object != null) copyKeysToAttributes(attributes,object,false);
raw.put("info",info);
raw.put("attributes",attributes);
return raw;
}
public Properties getAttributes(String path)
{
Properties attributes = (Properties)Common.readXMLObject(path+"attributes.XML");
return attributes;
}
public void updateMachineSyncDates(Properties credentials, String machineGuid) throws Exception
{
Properties attributes = getAttributes(home+"users/"+credentials.getProperty("username")+"/device/"+machineGuid+"/");
if (attributes == null) attributes = new Properties();
attributes.put("inservice","1");
if (!attributes.containsKey("firstsyncdate"))
{
attributes.put("firstsyncdate", sdf_createDate.format(new Date()));
}
attributes.put("lastsyncdate", sdf_createDate.format(new Date()));
Common common_code = new Common();
common_code.writeXMLObject(home+"users/"+credentials.getProperty("username")+"/device/"+machineGuid+"/attributes.XML",attributes,"props");
}
}