📄 getgreetingcardservlet.java
字号:
File fCropTmp = File.createTempFile("igc_cropimg_"+sID+"_", ext, new File(Ig3Config.GetValue("TMPUPLOAD_DIR")));
if(isMRCIMG)
{
boolean res = com.igm.image.ImageUtil.cropAndSave(sHome+sID+ext, fCropTmp.getAbsolutePath(),
CropInfo[0], CropInfo[1], CropInfo[2], CropInfo[3]);
if (!res)
{
doLog("[GetGreetingCardServlet] - Failed to crop image");
return null;
}
//Add on 11 April to resize multi-res large image file.
res = com.igm.image.ImageUtil.resizeAndSave(sHome+sID+ext, fCropTmp.getAbsolutePath(),
icon_width, icon_height);
if (!res)
{
doLog("[GetGreetingCardServlet] - Failed to resizeNsave image");
return null;
}
}
else
{
boolean res = com.igm.image.ImageUtil.resizeAndSave(sHome+sID+ext, fCropTmp.getAbsolutePath(),
icon_width, icon_height);
if (!res)
{
doLog("[GetGreetingCardServlet] - Failed to resizeNsave image");
return null;
}
}
doLog("[GetGreetingCardServlet] - Success to crop image");
outputFile = fCropTmp.getAbsolutePath();
}
catch (Exception exception)
{
StackTraceElement[] trace = exception.getStackTrace();
for (int i=0; i < trace.length; i++)
{
doLog(trace[i].toString());
}
}
return outputFile;
}
/**
* Deploy Jar File On-The-Fly
* @param table
* @param session
* @param inJad
* @param inJar
* @param userAgent
* @return
*/
private String deployJar(Hashtable table, String session, String inJad, String inJar, String userAgent)
{
SessionID deployKey = null;
String randKey = "";
String outJar = "";
String outJad = "";
String midletName = "";
String previousKey = "";
String mfstr = "";
FileInputStream fis = null;
FileOutputStream fos = null;
JarInputStream jis = null;
JarOutputStream jos = null;
Manifest mf = null;
ZipEntry zEntry = null;
boolean done = false;
try
{
deployKey = new SessionID((String)table.get("mt"));
randKey = deployKey.next(0);
outJar = IGCConfig.tmpHome + randKey + ".jar";
outJad = IGCConfig.tmpHome + randKey + ".jad";
midletName = (String)table.get("midletname");
previousKey = (String)table.get("deploy");
if(previousKey != null)
{
clearOutput(previousKey);
}
clearOutput(randKey);
fis = new FileInputStream(inJar);
jis = new JarInputStream(fis);
mfstr = "Manifest-Version: 1.0\n" +
"MicroEdition-Configuration: CLDC-1.0\n" +
"MIDlet-Name: " + midletName + "\n" +
"MIDlet-Vendor: Gen-X Technology\n" +
"MIDlet-1: " + midletName + ", /k.png, GreetingCardPlayer\n" +
"MIDlet-Version: 1.0\n" +
"MicroEdition-Profile: MIDP-1.0\n\n";
mf = new Manifest(new ByteArrayInputStream(mfstr.getBytes()));
fos = new FileOutputStream(outJar);
jos = new JarOutputStream(fos, mf);
jos.setMethod(ZipOutputStream.DEFLATED);
doLog("[GetGreetingCardServlet] - Loop into the incoming jar");
while((zEntry = jis.getNextEntry()) != null)
{
String name = zEntry.getName();
doLog("[GetGreetingCardServlet] - "+name+" found");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int cnt = -1;
byte[] data = new byte[1024];
doLog("[GetGreetingCardServlet] - start reading zip entry");
while((cnt = jis.read(data, 0, data.length)) != -1)
{
baos.write(data, 0, cnt);
}
doLog("[GetGreetingCardServlet] - done reading zip entry");
done = false;
if(name.equals("3.png"))
{
doLog("[GetGreetingCardServlet] - customizeMessage 3.png start");
if(customizeMessage(session, (String)table.get("msg"), jos, baos.toByteArray()))
{
doLog("[GetGreetingCardServlet] - customizeMessage DONE");
done = true;
}
}
baos.close();
if(!done)
{
doLog("[GetGreetingCardServlet] - "+name+" add to jar");
jos.putNextEntry(new ZipEntry(name));
doLog("[GetGreetingCardServlet] - "+name+" after put");
data = baos.toByteArray();
jos.write(data, 0, data.length);
doLog("[GetGreetingCardServlet] - "+name+" after write.");
}
}
doLog("[GetGreetingCardServlet] - Add Jar done..");
doLog("[GetGreetingCardServlet] - Extra Log..");
//Add in the image file (p0, p1 and p2)
doLog("[GetGreetingCardServlet] - imgid1 = "+table.get("imgid1"));
doLog("[GetGreetingCardServlet] - imgid1 = "+table.get("imgid2"));
doLog("[GetGreetingCardServlet] - imgid1 = "+table.get("imgid3"));
doLog("[GetGreetingCardServlet] - imgid1 = "+table.get("polyid"));
addNextEntry("p0.png", jos, read(getImage((String)table.get("imgid1"), userAgent, "0")));
doLog("[GetGreetingCardServlet] - Add Image 1 done..");
addNextEntry("p1.png", jos, read(getImage((String)table.get("imgid2"), userAgent, "1")));
doLog("[GetGreetingCardServlet] - Add Image 2 done..");
addNextEntry("p2.png", jos, read(getImage((String)table.get("imgid3"), userAgent, "2")));
doLog("[GetGreetingCardServlet] - Add Image 3 done..");
addNextEntry("1.mid", jos, read(getMidi((String)table.get("polyid"))));
doLog("[GetGreetingCardServlet] - Add Midi done..");
jis.close();
jos.close();
fis.close();
fos.close();
File fJar = new File(outJar);
doLog("[GetGreetingCardServlet] - Start to load JAD file..");
fis = new FileInputStream(inJad);
Properties p = new Properties();
p.load(fis);
Enumeration e = p.propertyNames();
RandomAccessFile fJad = new RandomAccessFile(outJad, "rw");
while(e.hasMoreElements())
{
String key = (String)e.nextElement();
String value = p.getProperty(key);
if(key.equals("MIDlet-Jar-Size"))
{
fJad.writeBytes("MIDlet-Jar-Size: " + fJar.length() + "\n");
}
else if(key.equals("MIDlet-Jar-URL"))
{
fJad.writeBytes("MIDlet-Jar-URL: http://www.iguanamobile.com/tmpupload/" + randKey + ".jar\n");
}
else if(key.equals("MIDlet-1"))
{
fJad.writeBytes("MIDlet-1: " + midletName + ", /k.png, GreetingCardPlayer\n");
}
else if(key.equals("MIDlet-Name"))
{
fJad.writeBytes("MIDlet-Name: " + midletName + "\n");
}
else
{
fJad.writeBytes(key + ": " + value + "\n");
}
}
fJad.writeBytes("MIDlet-Install-Notify: " + IGCConfig.downloadURL + "?S=" + session + "\n");
fJad.close();
fis.close();
doLog("[GetGreetingCardServlet] -JAD File created..");
if(!new File(outJad).exists() || !new File(outJad).exists())
{
doLog("[GetGreetingCardServlet] -JAD not exist");
return null;
}
return randKey;
}
catch(Exception exception)
{
StackTraceElement[] trace = exception.getStackTrace();
for (int i=0; i < trace.length; i++)
{
doLog(trace[i].toString());
}
doLog("[GetGreetingCardServlet] - Exception : "+exception.getMessage());
return null;
}
finally
{
try { jis.close(); } catch(Exception e) {}
try { jos.close(); } catch(Exception e) {}
try { fis.close(); } catch(Exception e) {}
try { fos.close(); } catch(Exception e) {}
}
}
/**
* Clear session and temp jar/jad file from tmp location
* @param table
* @param session
*/
private void clearAll(Hashtable table, String session)
{
String deployKey = "";
File fDelete = null;
try
{
deployKey = (String)table.get("deploy");
clearOutput(deployKey);
fDelete = new File(IGCConfig.tmpHome + session + IGCConfig.sessionExt);
fDelete.delete();
}
catch (Exception exception)
{
// TODO: handle exception
}
}
/**
* Remove the temp jar and jad file
* @param session
*/
private void clearOutput(String session)
{
String jad = IGCConfig.tmpHome + session + ".jad";
String jar = IGCConfig.tmpHome + session + ".jar";
File fOut = new File(jad);
try { fOut.delete(); } catch(Exception e) {}
fOut = new File(jar);
try { fOut.delete(); } catch(Exception e) {}
}
/**
* Create end-user customize message
* @param session
* @param msg
* @param jos
* @param data
* @return
*/
private boolean customizeMessage(String session, String msg, JarOutputStream jos, byte[] data)
{
doLog("[GetGreetingCardServlet] - Msg = "+msg);
ByteArrayInputStream bais = null;
ByteArrayOutputStream baos = null;
DataInputStream dis = null;
DataOutputStream dos = null;
try
{
if(msg == null || msg.length() == 0)
{
return false;
}
doLog("[GetGreetingCardServlet] - B4 ucsToAscii");
msg = ApicUtil.ucsToAscii(msg);
doLog("[GetGreetingCardServlet] - After ucsToAscii - Msg = "+msg);
bais = new ByteArrayInputStream(data);
dis = new DataInputStream(bais);
baos = new ByteArrayOutputStream();
dos = new DataOutputStream(baos);
doLog("[GetGreetingCardServlet] - Start write msg");
dos.writeUTF(msg);
doLog("[GetGreetingCardServlet] - Done write msg");
return addNextEntry("3.png", jos, baos.toByteArray());
}
catch(Exception exception)
{
StackTraceElement[] trace = exception.getStackTrace();
for (int i=0; i < trace.length; i++)
{
doLog(trace[i].toString());
}
return false;
}
finally
{
try { dis.close(); } catch(Exception e) {}
try { dos.close(); } catch(Exception e) {}
try { bais.close(); } catch(Exception e) {}
try { baos.close(); } catch(Exception e) {}
}
}
/**
* Add the next entry to a zip container
* @param name
* @param jos
* @param data
* @return
*/
private boolean addNextEntry(String name, JarOutputStream jos, byte[] data)
{
try
{
jos.putNextEntry(new ZipEntry(name));
jos.write(data, 0, data.length);
jos.flush();
jos.closeEntry();
return true;
}
catch(Exception e)
{
return false;
}
}
/**
* Do call back
* @param table
* @return
*/
private boolean doCallback(Hashtable table)
{
// return true if no callback is require
String url = (String)table.get("cburl");
if(url == null || url.length() == 0)
{
return true;
}
DataConnect dc = new DataConnect();
String pass = dc.querySQL("select password from phonelist where nickname=" +
"\'" + table.get("nick") + "\'", "password");
url += "?nick=" + table.get("nick") +"&pass=" + pass +"&mo=" +
table.get("mo") + "&mt=" + table.get("mt") + "&uid=" + table.get("uid");
return (ApicUtil.getURL(url) != null);
}
/**
* Log transaction/charge and rewards
* @param table
* @param id
* @param type
* @return
*/
private boolean logAction(Hashtable table, String id, String type)
{
doLog("[GetGreetingCardServlet] - logAction Started");
doLog("[GetGreetingCardServlet] - id = "+id);
doLog("[GetGreetingCardServlet] - type = "+type);
// if already hit, we no need to log, coz it shd have logged earlier
String hit = (String)table.get("hit");
doLog("[GetGreetingCardServlet] - hit = "+hit);
if(Integer.parseInt(hit) > 0)
{
return true;
}
String nick = (String)table.get("nick");
String mt = (String)table.get("mt");
String mccmnc = (String)table.get("mccmnc");
doLog("[GetGreetingCardServlet] - nick = "+nick);
doLog("[GetGreetingCardServlet] - mt = "+mt);
doLog("[GetGreetingCardServlet] - mccmnc = "+mccmnc);
doLog("[GetGreetingCardServlet] - Init TransactionLog");
TransactionLog txnLog = new TransactionLog();
doLog("[GetGreetingCardServlet] - call chargeDownloader");
if(txnLog.chargeDownloader(nick, mt, mccmnc, type, id))
{
doLog("[GetGreetingCardServlet] - call logDownloadCount");
txnLog.logDownloadCount(type, id);
doLog("[GetGreetingCardServlet] - call rewardCreator");
txnLog.rewardCreator(type, id);
doLog("[GetGreetingCardServlet] - return true");
return true;
}
return false;
}
/**
* Update the download hit in the session file
* @param session
* @param ua
* @param deployed
*/
private void updateHit(String session, String ua, String deployed)
{
FileInputStream fis = null;
Properties p = null;
String fname = "";
String hit = "";
int iHit = 0;
try
{
fname = IGCConfig.tmpHome + session + IGCConfig.sessionExt;
fis = new FileInputStream(fname);
p = new Properties();
p.load(fis);
hit = p.getProperty("hit");
fis.close();
iHit = Integer.parseInt(hit);
iHit++;
p.setProperty("deploy", deployed);
p.setProperty("hit", Integer.toString(iHit));
// log ua if this is the 1st hit for future
// comparision if this link is hit again
if(iHit == 1)
{
p.setProperty("ua", ua);
}
fis.close();
Enumeration e = p.propertyNames();
RandomAccessFile f = new RandomAccessFile(fname, "rw");
f.setLength(0L);
while(e.hasMoreElements())
{
String sKey = (String)e.nextElement();
String sValue = p.getProperty(sKey);
f.writeBytes(sKey + "=" + sValue + "\r\n");
}
f.close();
}
catch(Exception exception)
{
StackTraceElement[] trace = exception.getStackTrace();
for (int i=0; i < trace.length; i++)
{
doLog(trace[i].toString());
}
}
finally
{
try { fis.close(); } catch(Exception e) {}
}
}
/**
* Read a Jad File
* @param jad
* @return
*/
public static String readJad(String jad)
{
try
{
RandomAccessFile f = new RandomAccessFile(jad, "r");
byte[] content = new byte[(int)f.length()];
f.read(content);
f.close();
return new String(content);
}
catch(Exception e)
{}
return null;
}
/**
* Read a file in byte array
* @param file
* @return
* @throws IOException
*/
public byte[] read(String file) throws IOException
{
InputStream in = new FileInputStream(file);
byte[] data = new byte[in.available()];
in.read(data);
in.close();
return data;
}
/**
* Write log into file
* @param msg
*/
private void doLog(String msg)
{
try
{
RandomAccessFile f = new RandomAccessFile(IGCConfig.logFile+CLASSNAME+"_"+
ApicUtil.TimeStr("yyyyMMdd")+".log", "rw");
f.seek(f.length());
f.writeBytes(ApicUtil.TimeStr("[HH:mm:ss]") + msg + "\r\n\r\n");
f.close();
}
catch(Exception e)
{}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -