Friday, January 24, 2014

Accessing Postgres UUID with JPA

http://stackoverflow.com/questions/11284359/persisting-uuid-in-postgresql-using-jpa

JPA 2.1 provides a very easy way to use the PostgreSQL uuid column type and java.util.UUID as the type of the corresponding entity field:
@javax.persistence.Converter(autoApply = true)
public class PostgresUuidConverter implements AttributeConverter<UUID, UUID> {

    @Override
    public UUID convertToDatabaseColumn(UUID attribute) {
        return attribute;
    }

    @Override
    public UUID convertToEntityAttribute(UUID dbData) {
        return dbData;
    }

}
Just add this class to your persistence configuration and annotate UUID fields with@Column(columnDefinition="uuid").

Requests.py

I needed to do some RESTful calls via command line.
Naturally, I could have used curl, but I wanted to save my commands.

I remember I used Python a long time ago, so I was investigating curling via Python. There is a pycurl library, but this one seemed simple and fun.

To set up,

Get the Code

Requests is actively developed on GitHub, where the code is always available.
You can either clone the public repository:
git clone git://github.com/kennethreitz/requests.git
Download the tarball:
$ curl -OL https://github.com/kennethreitz/requests/tarball/master
Or, download the zipball:
$ curl -OL https://github.com/kennethreitz/requests/zipball/master
Once you have a copy of the source, you can embed it in your Python package, or install it into your site-packages easily:
$ python setup.py install

Super simple, very fun!
It just works!

Sunday, January 12, 2014

Secure JAX-RS REST endpoints

Doesn't at all qualify for a 5 minute Google, but
searching "spring security jersey example"
gave me the following two links that are important.

http://stackoverflow.com/questions/18086048/jax-rs-how-to-secure-rest-endpoints
http://stackoverflow.com/questions/6795568/jersey-jax-rs-spring-security-application-sample

Saturday, January 11, 2014

Grabbing ajax parameters in Jersey

According to the website http://docs.oracle.com/cd/E19798-01/821-1841/gipzz/index.html:


To obtain a general map of parameter names and values for query and path parameters, use the following code:
@GET
public String get(@Context UriInfo ui) {
    MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
    MultivaluedMap<String, String> pathParams = ui.getPathParameters();
}
The following method extracts header and cookie parameter names and values into a map:
@GET
public String get(@Context HttpHeaders hh) {
    MultivaluedMap<String, String> headerParams = ui.getRequestHeaders();
    Map<String, Cookie> pathParams = ui.getCookies();
}
In general, @Context can be used to obtain contextual Java types related to the request or response.
For form parameters, it is possible to do the following:

@POST
@Consumes("application/x-www-form-urlencoded")
public void post(MultivaluedMap<String, String> formParams) {
    // Store the message
}

http://docs.oracle.com/cd/E19798-01/821-1841/gipzz/index.html
info about jax-rs applications here
http://docs.oracle.com/cd/E19798-01/821-1841/gipzz/index.html

Wednesday, January 8, 2014

Adding new users to postgres

http://www.cyberciti.biz/faq/howto-add-postgresql-user-account/

Access postgres with database you want to share,
do
CREATE USER new_user WITH PASSWORD 'password'; 
and then

GRANT ALL PRIVILEGES ON DATABASE database to new_user;

Tuesday, January 7, 2014

No 'Access-Control-Allow-Origin' problem...

I never learned how to map my localhost:8000 to localhost and localhost:8080 to localhost to avoid this problem...