Angular helper to encode object as www/formdata

Helper to serialize data to www/formdata – needed for turning a javascript object into encoded form data to POST:

Apache Shiro – HTTP auth and Form auth in same project

I wanted a way to do regular form auth with an application. The idea is that regular users would use form authentication, and API users could use Simple HTTP authorization.

I had a REST service that I exposed at 2 different URLs by creating 2 Application classes extending javax.ws.rs.Application and using

javax.ws.rs.ApplicationPath;

to expose 2 different URLs. The first is at /rest and the second is at /api. Now, in the shiro.ini I setup a different filter for each path, and to access /rest you have to be logged in via form auth, and to access /api you must be logged in with HTTP Simple authentication.

I implemented a custom AuthorizationRealm and added the api role to the users that can access the API.

That works great, and simplifies any API connections by allowing Simple auth. Simple auth should also force SSH because it is not secure over clear HTTP, but that’s well documented by the Shiro project. There will be a post coming soon about how to implement a custom AuthorizingRealm to use your own DAOs to lookup users, but it is only 3 methods and is pretty straightforward.

PHP Validate Google CAPTCHA

Put this in check.php (or whatever) and call -> http://myserver/php/stuff/check.php?response=CAPTCHA_VALUE_FROM_PAGE, it will return JSON to the caller.

Here is a Javascript method (using JQuery) to do this validation:

Finally, here is the form that uses it:

Angular Directives – compile & link & trusted HTML

More information on Angular directives:

Here’s a simple directive with a couple of scope variables that get displayed:

Here is the angular stuff:

Here is a fiddle for the example, which has some css and things. Nothing really special here.

Now, in order to make some DOM changes, we can’t change the element during the link function. If you want to change the element, you have to do it during the compile function. The compile function gets called when the directive is created, and should return the link function. This allows the directive to be modified. This example shows how to add a list to the directive.

We can also add some HTML with the “sce” module:

The interesting thing there is the compile function. The compile function gets the tElem, which is the template element itself. This function is run when the directive is compiled, and there is no scope available. We can, however, add some elements to the directive. Note that the compile function returns the link function.

Here is a working fiddle

Here’s a final fiddle showing how to add a list to the element’s div

XCode Swift GET Request and Parse

Angular

Delete/Modify all Jenkins CI jobs

Navigate to

http://jenkins-ci/script

then, to delete all jobs, run:

To filter and disable jobs, run:

The Jenkins API docs are here

Python Logging Annotation – Automatically create logger for methods

This is a pattern for creating an annotation that will “inject” a logger into a python method. This allows you to decorate a python method with @logged and you will have a variable named logger created with your method name.

This is nice because you do not have to have boilerplate code all over the place.  It also allows you to easily have a logger for each function instead of a global logger.  This makes reading logs much easier since you can tell exactly where your logging call was made.

Here is an outline of the process, the comments in the code explain where these steps happen:

  1. Get the global variables for the decorated function
  2. If there is an existing logger in the function, we save it
  3. Create a new logger with the function name as the logger name
  4. Set the new logger as a global variable for the function
  5. Call the function
  6. Restore the existing logger as a global

Now your methods will look like this: