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...

Note to Self... Java Web Projects

I've had to relearn this for the second time, so I'll write about it.

I installed netbeans on my mac and am using JEE5 (:P), and Netbeans 7.3.1

Create a Java Web > Web Application,
and choose the Spring configuration (because I felt like it... I'm not sure if I will use it yet, but Spring plugins are attractive)

Then, go to terminal and create a few tables on postgres. Put some fake data in them.

Go back and create entities from database. Connect to the db correctly, localhost:5432 blah blah.
On the 3rd page (or something), don't use information_schema, use public. That's where the tables are.

Then, create RESTful service from entities. They'll make a bunch of RESTFacade files (in JEE6 it made me resource classes...). Then, get rid of the aopalliance error by downloading the jar and adding it to your jars. Also, add your jdbc driver into your jars.

After that, you can run and deploy it on your tomcat server (which you should have hooked up to your netbeans ide)

ALSO, to make it output JSON instead of XML, download the jersey-json jar here.

Nobody's gonna find this helpful, this was a note to self.

Don't say "but"

An interesting Quora article...

"A very smart woman I worked with once told me that if I eliminated the word "but" from my professional vocabulary, I'd find greater acceptance for my ideas, and greater cooperation from my team members. She said people would have a very different perception of me if I could change this one thing.

The reason, she said, is because the word "but" negates everything that precedes it, and you cast a negative spin on anything you say when you use it. 

Consider, for example, "We can do it this way, but it'll be way too expensive given our budget," versus "We can do it this way, and if we do, we'll need to cut back on other important features." The first indicates that we can't even consider the option. The second acknowledges possibility and describes consequences.

"But" is exclusive and isolating, "and" is inclusive and welcoming.

She was absolutely right, and it's advice I have used with great success for the past 30 years of my life."

Saturday, January 4, 2014

Securrrty

http://web.mit.edu/kerberos/www/dialogue.html

A really nice story to help understand Kerberos security (and another by-product of trying to clean out my inbox)

Magnus on USChess

A non-coding related post, but here's an interesting (set of) video(s) on early Carlsen

http://main.uschess.org/content/view/11623/658/


Code for a baby...

I like to email myself stuff back that I've done. (As a result, I have a super cluttered inbox).

Here's an email that I found from 4/4/12 when I just started js.