Jul 31
Saturday
DZone Snippets
Send sms (worldwide) using www.smsmatrix.com in Java PDF Print E-mail
Thursday, 29 July 2010 22:46
Send sms (worldwide) using www.smsmatrix.com.
More examples at SMS Gateway page.


try
{
String MATRIXURL = "http://www.smsmatrix.com/matrix";
String PHONE = "12506063167";
String USERNAME = " This e-mail address is being protected from spambots. You need JavaScript enabled to view it ";
String PASSWORD = "pass72727";
String TXT = "This is a test, pls ignore";

String q = "username=" + URLEncoder.encode (USERNAME, "UTF-8");
q += "&" + "password=" + URLEncoder.encode (PASSWORD, "UTF-8");
q += "&" + "phone=" + PHONE;
q += "&" + "txt=" + URLEncoder.encode (TXT, "UTF-8");

URL url = new URL (MATRIXURL);
URLConnection conn = url.openConnection();
conn.setDoOutput (true);
OutputStreamWriter wr = new OutputStreamWriter (conn.getOutputStream());
wr.write (q);
wr.flush();

BufferedReader rd = new BufferedReader (new InputStreamReader (conn.getInputStream()));
String line;
System.out.println ("Matrix API Response :");
while ((line = rd.readLine()) != null) { System.out.println (line); }
wr.close();
rd.close();
} catch (Exception e) { }

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/suq-pD8hi0s/11953

 
Convert hash conditions to array conditions (required by find_by_tsearch) PDF Print E-mail
Wednesday, 28 July 2010 13:06
Inspired by http://snippets.dzone.com/posts/show/6839


class Hash
def to_array_conditions
[self.keys.map{|k| "#{k} = ?" }.join(" AND "), self.values].flatten
end
end

test "convert hash conditions to array conditions" do
assert_equal ['city = ? AND country_code = ? AND state = ?',
"Adendorf", "DE", "Niedersachsen"], {
:country_code => "DE",
:state => "Niedersachsen",
:city => "Adendorf"
}.to_array_conditions
end

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/y6Uigqel1FQ/11949

 
The 12 Hour Toothache Cure PDF Print E-mail
Wednesday, 28 July 2010 10:14
If you are struggling with a toothache, you know how painful it is. The agony of a toothache can be so troubling that you are not able to focus on anything else. Here I am going to show you the 12 hour toothache cure.

Bacterial infection is the cause of most tooth pain. There are many ways to increase the risk of a painful toothache, one of which is habitual eating.

Make a mixture of a little black pepper powder and salt. Apply gently to the infected tooth and gums. You'll feel better nearly instantly, and utilizing it every day can strengthen gums and keep them from bleeding.

Lemon is also effective in curing toothache. If you like the lime test, apply it to the cavity and you will forget the pain within minutes. It's also good for your body too.

To alleviate toothache, chew on a raw onion for 3-5 minutes until you get pain relief. Onion has some bactericidal properties which left no bacteria behind it.

Chewing spinach leave can also give you tooth ache relief - it will make your gums stronger and fight against bad breath. Mix around five grams of peppermints with a cup of water and drink it, and you will feel relief from toothache. In the morning before you eat anything else, you can also try chewing the leaves of the guava tree. This works for all kinds of tooth pain.

Above are the simple yet effective home remedies to stop a toothache.

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/0_CUvcfG2L4/11947

 
use maven ant plugin to run python test PDF Print E-mail
Wednesday, 28 July 2010 07:45
Started to use python for some system stuff and wanted to be able to test it with the rest of the code.

The ant plugin is an EZ way to shell out from pom.xml



org.apache.maven.plugins
maven-antrun-plugin


test











run




Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/IFUUDoh97Cc/11945

 
130 lines web server PDF Print E-mail
Tuesday, 27 July 2010 13:00
Here a very tiny web server

Warning! stop_browser() must be complete...


require 'socket'
require 'thread'

#################### Tiny embeded webserver
class Webserver
def info(txt) ; puts "nw>i> #{txt}" ; end
def error(txt) ; puts "nw>e> #{txt}" ; end
def unescape(string) ; string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2}))/n) { [$1.delete('%')].pack('H*') } ; end

def initialize(port=7080,cadence=10,timeout=120)
@last_mtime=File.mtime(__FILE__)
@port=port
@timeout=timeout
@th={}
@cb={}
@redirect={}
info("serveur http #{port} ...")
@server = TCPServer.new('0.0.0.0', @port)
@server.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
info("serveur http #{port} ready!")
observe(cadence,120)
Thread.new {
begin
while (session = @server.accept)
Thread.new(session) do |sess|
@th[Thread.current]=Time.now
request(sess)
@th.delete(Thread.current)
end
end
rescue
error($!.to_s)
end
}
end
def serve(uri,&blk)
@cb[uri] = blk
end
def observe(sleeping,delta)
Thread.new do loop do
sleep(sleeping)
nowDelta=Time.now-delta
l=@th.select { |th,tm| (tm l.each { |th,tm| info("killing thread") ; th.kill; @th.delete(th) }
end ; end
end
def request(session)
request = session.gets
uri = (request.split(/\s+/)+['','',''])[1]
#info uri
service,param,*bidon=(uri+"?").split(/\?/)
params=Hash[*(param.split(/#/)[0].split(/[=&]/))] rescue {}
params.each { |k,v| params[k]=unescape(v) }
uri=unescape(service)[1..-1].gsub(/\.\./,"")
userpass=nil
if (buri=uri.split(/@/)).size>1
uri=buri[1..-1].join("@")
userpass=buri[0].split(/:/)
end
do_service(session,request,uri,userpass,params)
rescue
error("Error Web get on #{request}: \n #{$!.to_s} \n #{$!.backtrace.join("\n ")}" ) rescue nil
session.write "HTTP/1.1 501/NOK\rContent-type: text/html\r\n\r\nDZone Snippets | NewsError : #{$!}" rescue nil
ensure
session.close rescue nil
end
def redirect(o,d)
@redirect[o]=d
end
def do_service(session,request,service,user_passwd,params)
#info "request='"+service+"'"
redir=@redirect["/"+service]
service=redir.gsub(/^\//,"") if @redirect[redir]
if redir && ! @redirect[redir]
do_service(session,request,redir.gsub(/^\//,""),user_passwd,params)
elsif @cb["/"+service]
begin
code,type,data= @cb["/"+service].call(params)
if code==0 && data != '/'+service
do_service(session,request,data[1..-1],user_passwd,params)
else
code==200 ? sendData(session,type,data) : sendError(session,code,data)
end
rescue
puts "Error in get /#{service} : #{$!}"
sendError(session,501,$!.to_s)
end
elsif service =~ /^stop/
sendData(session,".html","Stopping...");
Thread.new() { sleep(0.1); stop_browser() }
elsif File.directory?("./"+service)
sendData(session,".html",makeIndex("./"+service))
elsif File.exists?(service)
sendFile(session,service)
else
info("unknown request serv=#{service} params=#{params.inspect} #{File.exists?(service)}")
sendError(session,500);
end
end
def stop_browser
info "exit on web demand !"
@serveur.close rescue nil
exit!(0)
end
def makeIndex(dir)
dirs,files=Dir.glob(dir=="./" ? "*" :dir+"/*").sort.partition { |f| File.directory?(f)}
updir = dir.split(/\//)[0..-2].join("/")
dirs=[updir]+dirs if updir.length>0
"

Repertoire #{dir}



#{to_table(dirs.map {|s| ""+s+"/"+""})}
#{to_tableb(files) {|f| [LICON[rand(LICON.size)],""+File.basename(f)+"",File.size(f),File.mtime(f).strftime("%d/%m/%Y %H:%M:%S")]}}"
end
def to_table(l)
"#{l.map {|s| ""}.join("")}
#{s}
"
end
def to_tableb(l,&bl)
"#{l.map {|s| ""}.join("")}
#{bl.call(s).join("")}
"
end
def sendError(sock,no,txt=nil)
if txt
txt="
#{txt}"

end
sock.write "HTTP/1.1 #{no}/NOK\rContent-type: #{mime(".html")}\r\n\r\n

Error #{no} : #{txt}

"
end
def sendData(sock,type,content)
sock.write "HTTP/1.1 200/OK\rContent-type: #{mime(type)}\r\nContent-size: #{content.size}\r\n\r\n"
sock.write(content)
end
def sendFile(sock,filename)
sock.write "HTTP/1.1 200/OK\rContent-type: #{mime(filename)}\r\nContent-size: #{File.size(filename)}\r\n\r\n"
File.open(filename,"rb") { |f| sock.write(f.read) }
end
def mime(string)
MIME[string.split(/\./).last] || "data-octet-stream"
end
LICON="☀☃☎☑☑☠☣☮☺☂".split(/;/).map {|c| c+";"}
MIME={"png" => "image/png", "gif" => "image/gif", "html" => "text/html","htm" => "text/html",
"js" => "text/javascript" ,"css" => "text/css","jpeg" => "image/jpeg" ,"jpg" => "image/jpeg" ,
"pdf"=> "application/pdf" , "svg" => "image/svg+xml","svgz" => "image/svg+xml",
"xml" => "text/xml" ,"xsl" => "text/xml" ,"bmp" => "image/bmp" ,"txt" => "text/plain" ,
"rb" => "text/plain" ,"pas" => "text/plain" ,"tcl" => "text/plain" ,"java" => "text/plain" ,
"c" => "text/plain" ,"h" => "text/plain" ,"cpp" => "text/plain", "xul" => "application/vnd.mozilla.xul+xml"
}

end # 130 loc webserver :)



Use it with :


$ws=Webserver.new(7007,1,120)


$ws.serve "/index" do |params|
[200,".html","hello!"]
end

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/xqf9Ddx79s0/11943

 
<< Start < Prev 1 2 3 4 5 6 7 8 9 10 Next > End >>

Page 1 of 150