Here is the source code to the PostBack plugin. Someone may find this useful in developing their own plugins. Same rules on how to make the package structure. (Third Party)
Start.java
package PostBack; import java.util.*; import java.io.*; import java.net.*; import javax.swing.*; import crushftp.handlers.Common; import crushftp.server.ServerStatus; public class Start implements Serializable { Properties settings = new Properties(); String version = "4.1"; GUI g = null; public Properties getDefaults() { Properties p = new Properties(); p.put("enabled","false"); p.put("debug","false"); p.put("send_alerts","false"); p.put("url",""); p.put("retry_count","0"); p.put("max_full_speed","10"); p.put("delay_interval","3000"); p.put("username",""); p.put("password",""); p.put("post_data",""); p.put("recent","false"); p.put("version",version); return p; } public void setSettings(Properties p) throws Exception { settings = p; if (g != null) g.setSettings(settings); } public Properties getSettings() { if (g != null) settings = g.getSettings(); settings.put("version",version); return settings; } public javax.swing.JPanel getPrefsPanel() { if (g == null) g = new GUI(); g.setParent(this); g.setSettings(settings); return g; } public String replace_vars(String the_line, Properties user_info, Properties p,crushftp.server.ServerSession thisSession) { try{the_line = Common.replace_str(the_line, "%user_name%", URLEncoder.encode(user_info.getProperty("user_name",""), "UTF-8"));}catch(Exception e){} try{the_line = Common.replace_str(the_line, "%user_ip%", URLEncoder.encode(user_info.getProperty("user_ip",""), "UTF-8"));}catch(Exception e){} String path = ""; try{path = new URL(p.getProperty("url","")).getPath();}catch(Exception e){} if (Common.machine_is_windows()) try{path = new File(path).getCanonicalPath();}catch(Exception e){} try{the_line = Common.replace_str(the_line, "%the_real_path%", URLEncoder.encode(path, "UTF-8"));}catch(Exception e){} try{the_line = Common.replace_str(the_line, "%the_file_path%", URLEncoder.encode(p.getProperty("the_file_path",""), "UTF-8"));}catch(Exception e){} try{the_line = Common.replace_str(the_line, "%the_file_name%", URLEncoder.encode(p.getProperty("the_file_name",""), "UTF-8"));}catch(Exception e){} the_line = Common.replace_str(the_line, "%the_file_size%", p.getProperty("the_file_size","")); the_line = Common.replace_str(the_line, "%the_file_speed%", p.getProperty("the_file_speed","")); the_line = Common.replace_str(the_line, "%the_file_start%", p.getProperty("the_file_start","")); the_line = Common.replace_str(the_line, "%the_file_end%", p.getProperty("the_file_end","")); the_line = Common.replace_str(the_line, "%the_file_md5%", p.getProperty("the_file_md5","")); the_line = Common.replace_str(the_line, "%the_command%", p.getProperty("the_command","")); try{the_line = Common.replace_str(the_line, "%the_file_error%", URLEncoder.encode(p.getProperty("the_file_error",""), "UTF-8"));}catch(Exception e){} try{the_line = thisSession.server_status_frame.change_vars_to_values(the_line,thisSession);}catch(Exception e){} return the_line; } public Object run(Properties info) { if (!settings.getProperty("enabled").equalsIgnoreCase("true")) return null; String error = ""; try { if (info.getProperty("action","").equals("event")) //we only get called with an event if this plugin was targeted for an event { Vector items = (Vector)((Vector)info.get("items")).clone(); Properties user = (Properties)info.get("user"); Properties user_info = (Properties)info.get("user_info"); msg("item list size:" +items.size()); msg("items:" +items); int processed = 0; int x = 0; while(x<items.size()) { if (settings.getProperty("recent","").equals("true")) x = items.size()-1; Properties p = (Properties)items.elementAt(x); msg(p.toString()); String url = replace_vars(settings.getProperty("url"),user_info,p,(crushftp.server.ServerSession)info.get("ServerSession")); String post_data = replace_vars(settings.getProperty("post_data",""),user_info,p,(crushftp.server.ServerSession)info.get("ServerSession")); msg("url before:"+settings.getProperty("url")); msg("post_data before:"+settings.getProperty("post_data")); msg("url after:"+url); msg("post_data after:"+post_data); error = new java.util.Date()+"\r\n\r\nPostBack plugin failed:\r\n"; error += "post_data:"+post_data; for (int xx=0; xx<Integer.parseInt(get("retry_count"))+1; xx++) { try { Common.debug(0,"PostBack: url="+url); HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection(); connection.setDoOutput(true); connection.setDoInput(true); if (get("username").trim().length() > 0) { String authString = get("username")+":"+get("password"); String auth = "Basic " + new sun.misc.BASE64Encoder().encode(authString.getBytes()); connection.setRequestProperty("Authorization", auth); } byte dataBytes[] = post_data.getBytes(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Length", dataBytes.length+""); OutputStream os = connection.getOutputStream(); os.write(dataBytes); os.flush(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String decodedString = ""; while ((decodedString = in.readLine()) != null) { msg(decodedString); } in.close(); break; } catch(Exception e) { Common.debug(0,"PostBack: Error="+e.getMessage()); msg(e); error += "\r\nError:"+e.toString(); if (xx == Integer.parseInt(get("retry_count"))) { if (get("send_alerts").equals("true")) { crushftp.server.ServerSession thisSession = (crushftp.server.ServerSession)info.get("ServerSession"); ServerStatus.thisObj.runAlerts("pluginMessage_"+error,thisSession); } } else { Thread.sleep(10000); } } if (settings.getProperty("debug").equals("true")) { String s = "PostBack\r\nThis url was just called:\r\n"+url+"\r\n\r\n"+post_data; msg(s); } } if (processed++ > Integer.parseInt(get("max_full_speed"))) Thread.sleep(Integer.parseInt(get("delay_interval"))); x++; } } } catch(Exception e) { msg(e); error += "\r\nError:"+e.toString(); if (get("send_alerts").equals("true")) { crushftp.server.ServerSession thisSession = (crushftp.server.ServerSession)info.get("ServerSession"); ServerStatus.thisObj.runAlerts("pluginMessage_"+error,thisSession); } } return null; } public void testSettings() { String error = "OK"; JOptionPane.showMessageDialog(null, error, "Alert",JOptionPane.ERROR_MESSAGE); } public String get(String key) { return settings.getProperty(key); } public void msg(String s) { if (settings.getProperty("debug").equals("true")) Common.debug(0,"PostBack:"+s); } public void msg(Exception e) { if (settings.getProperty("debug").equals("true")) Common.debug(0,e); } }
Add new attachment
Only authorized users are allowed to upload new attachments.
«
This particular version was published on 05-Dec-2023 05:32 by Ben Spink.
G’day (anonymous guest)
Log in
JSPWiki