Wednesday, September 12, 2012

Spring Async and Future

I had a pesky reporting job that was going to be slow no matter what sql-fu I was going to come up with.  I knew that Spring had asynchronous tasks which I had used before but they kinda sucked.  Lots of manual bean building and xml config nasties.  But behold, I came across a nice annotation I was not aware of:

@Async


And I was off to the races.

I'll update with my example later.  For now, I give you the best blog entry to be found as of yet:

http://blog.inflinx.com/2012/09/09/spring-async-and-future-report-generation-example/

Thanks, Random Thoughts.


Tuesday, April 10, 2012

AVPlayer audio problems?

I'm finally working on an iphone app.  Yes, welcome to 2008!  And this one goes out to anyone who can't get their audio to play on their device, but it will work on simulators!  Fun!

Grrrrrr.

So here's the magic line of objective c that will get your video to play sound on a device.  Are you ready?  Here goes:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];

I'm staring at this line of code.  It appears to say that any errors with audio, ignore them.  So now your audio will work.  Yay, Apple.  This is the future of app developement?

Sunday, January 29, 2012

ReCaptcha without plugins

So I'm not that fantastic at reading documentation.  But let's face it.  Coders are not that great at spelling things out.  Exhibit A:

http://code.google.com/apis/recaptcha/docs/verify.html

There is nothing on this API explanation that explains that you can't make this call from javascript.  Although I should have known that putting both public and private keys in JS seems a little insecure.

Ulitimately, you gotta make the reCaptcha verification call from the server-side.  Since I'm a java/spring guy I made a controller that does that.  This is the step that would have been obvious if I just looked more carefully at the example plugins.  But still.

@Controller
public class RecaptchaController {

    public static final String PUBLIC_KEY = "XXX";
    private static final String PRIVATE_KEY = "YYY";
    private static final String RECAPTCHA_URL = "http://www.google.com/recaptcha/api/verify";
   
    @RequestMapping(value="/recaptcha", method=RequestMethod.POST)
    public void checkServer(@RequestParam("remoteip") String remoteip, @RequestParam("challenge") String challenge,
                            @RequestParam("response") String response, HttpServletResponse httpResp){
           
        HttpClient httpClient = new HttpClient();
        PostMethod post = new PostMethod( RECAPTCHA_URL );

        post.addParameter("privatekey", PRIVATE_KEY);
        post.addParameter("remoteip", remoteip);
        post.addParameter("challenge", challenge);
        post.addParameter("response", response);
       
        String resp = "false";
        try {
            httpClient.executeMethod(post);
            resp = getResponseAsString(post);
        } catch (Exception e) {
            resp = "false";
        }
       
        if(resp.startsWith("false")){
            resp = "ERROR";
        }else{
            resp = "SUCCESS";
        }
       
        try {
            OutputStreamWriter writer = new OutputStreamWriter( httpResp.getOutputStream() );
            writer.write(resp);
            writer.close();
        } catch (IOException e) {
            // do nothing
        }
       
    }
   
    private String getResponseAsString(HttpMethod hm) throws IOException {
        InputStream inputStream = hm.getResponseBodyAsStream();

        StringWriter writer = new StringWriter();
        IOUtils.copy(inputStream, writer, "UTF-8");

        return writer.toString();
    }  
}