Search in this blog

Monday, July 13, 2015

MyBatis-Spring Summary for Production Applications

MyBatis is my favorite ORM, when I first tried to use it I got surprised because the configuration was a really easy task.

I use it in every app with a relational database, I combine it with Spring as a Dependency Injection framework.

Most of the configuration I will write is using Annotations, read more about MyBatis-Spring to get a better understanding.

The next beans are for the Spring XML file configuration:

    <!-- DataSource -->
    <bean id="dataSource"
          class="org.apache.tomcat.jdbc.pool.DataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/dbname" />
        <property name="username" value="user" />
        <property name="password" value="password" />
        <property name="maxWait" value="15" />
        <property name="removeAbandonedTimeout" value="15" />
        <property name="defaultAutoCommit" value="false" />
    </bean>


You should define a dataSource, I use Tomcat Connection Pool, you can use the one you prefer.

Define the sqlSessionFactory:

    <!-- MyBatis config -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>


Defina a mapper bean (the one with the methods and sql queries):

    <!-- MyBatis mappers -->
    <bean id="comercioMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.alex.mapper.SampleMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>


Create the Interface for com.alex.SampleMapper:

package com.alex.mapper;

@Service
public interface SampleMapper {
 // the code later
}


Create a sample POJO:

public class Sample {
 private Integer id;
 private String name;
 private Integer age;
 // getters / setters
}


Assume you have a table like:

CREATE TABLE sample (
 id SERIAL NOT NULL PRIMARY KEY,
 name VARCHAR(50) NOT NULL,
 age INT NOT NULL
);


Declare methods to Add / Update / Delete:

    @Insert( "INSERT INTO sample (name, age) VALUES ( #{s.name), #{s.age) )" )
    @Options(useGeneratedKeys = true, keyProperty = "s.id")
    void add(@Param("s") Sample s);

    @Delete( "DELETE FROM sample WHERE id = #{s.id)" )
    void delete(@Param("s") Sample s);

    @Update( "UPDATE sample SET name = #{s.name}, age = #{s.age} WHERE id = #{s.id}" )
    void update(@Param("s") Sample s);

    @Select( "SELECT * FROM sample WHERE id = #{id}" )
    Sample find(@Param("id") Integer id);


The important part here is the "add" method which will try to insert a new row in a table with auto generated keys, if succeed, the generated key will be stored in the id field of the stored object.

This is really good, but what about is your the fields in your object have different names of the table in the database?

For example:

public class Sample {
 Integer id;
 String theName;
 Integer age;
}


You have to tell MyBatis for handling "theName" field as "name" column:

    @Select( "SELECT * FROM sample WHERE id = #{id}" )
    @Results({
        @Result(property = "theName", column = "name")
    })
    Sample find(@Param("id") Integer id);


What about reading complex objects?

public class Sample {
 Integer id;
 Data data,
}

public class Data {
 String name;
 Integer age;
}


Is the same, can you see it?

    @Select( "SELECT * FROM sample WHERE id = #{id}" )
    @Results({
        @Result(property = "data.name", column = "name"),
        @Result(property = "data.age", column = "age")
    })
    Sample find(@Param("id") Integer id);


Have you heard about TypeHandler? What about it?

The objects:

public class Sample {
 Integer id;
 Other other;
}

public class Other {
 Integer id;
}


The table:

CREATE TABLE sample (
 id INT NOT NULL PRIMARY KEY,
 other_id INT NOT NULL
)


The mapper:

    @Select( "SELECT * FROM sample WHERE id = #{id}" )
    @Results({
        @Result(
          property = "other.id", column = "other_id", typeHandler = OtherTypeHandler.class
        )
    })
    Sample find(@Param("id") Integer id);


Define the TypeHandler:

public class OtherTypeHandler extends BaseTypeHandler<Other> {
    @Override
    public Other getNullableResult(ResultSet rs, String colName) throws SQLException {
        Other other = new Other();
        other.setId(rs.getInt(colName));
        return other;
    }

    @Override
    public Other getNullableResult(ResultSet rs, int colNum) throws SQLException {
        Other other = new Other();
        other.setId(rs.getInt(colNum));
        return other;
    }

    @Override
    public Other getNullableResult(CallableStatement cs, int colNum) throws SQLException {
        Other other = new Other();
        other.setId(cs.getInt(colNum));
        return other;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Other t, JdbcType jt) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}


These are all the things I had needed using MyBatis, in the moment I needed I invest some time to find them, I put it here for using them if I forget them and hoping can be useful for you and save your time.

See you in the next post.

Sunday, July 12, 2015

Java Geocoding using Google Maps Api

Java Server-Side Geocoding

I was developing a web app including a map and some markers in it, I got a decent database having many human readable addresses like "1600 Amphitheatre Parkway, Mountain View, CA", in order to put a marker in the map for a human readable address you need coordinates, Geocoding is the process of for converting human readable address into geographic coordinates (latitude, longitude).

My app is developed in Java using Google Maps, Google has a API for geocoding (https://developers.google.com/maps/documentation/geocoding/), the problem is, the API works for JavaScript only, you can find lots of examples for using it this way.

Google has a web service which can give you geocodes in json or xml, try this link: http://maps.googleapis.com/maps/api/geocode/json?address=california&sensor=false

Here we are requesting the location of "california".

Now make in it works in Java is not a hard taks, you need to write the necessary objects for representing the json structure and some a method to do a GET request and parse the json result into the Java object.

I will use Apache Http Components for doing the request and Jackson JSON Processor for parsing the request.

First, declare the requiered objects for the geocoding API (all of them should have getters and setters, DONT FORGUET TO PUT IT).

public class GoogleGeoCode {
    private String status;
    private GoogleGeoResult [] results;
    private Boolean exclude_from_slo;
    private String error_message;
}
 
public class GoogleGeoResult   {
    private GoogleGeoAdressComponent [] address_components;
    private String formatted_address;
    private GoogleGeoGeometry geometry;
    private Boolean partial_match;
    private String place_id;
    private String [] types;
 

public class GoogleGeoAdressComponent {
    private String long_name;
    private String short_name;
    private String [] types;
}
 
public class GoogleGeoGeometry {
    private GoogleGeoBounds bounds;
    private GoogleGeoLatLng location;
    private String location_type;
    private GoogleGeoBounds viewport;
}
  
public class GoogleGeoBounds   {
    private GoogleGeoLatLng northeast;
    private GoogleGeoLatLng southwest;
}
 
public class GoogleGeoLatLng {
    private String lat;
    private String lng;
 

Before doing the next I hope you had read the documentation of Google for geocoding, if not, reading it would help you to understand some things.

Google allows you to do geocoding without having an API_KEY but in most cases having it would be better, if you will use the API_KEY you should do the request using SSL (https), if you sent the KEY over HTTP, google will reject the request, the method works for BOTH (http and https):

/**
 * Given an address asks google for geocode
 *
 * If ssl is true API_KEY should be a valid developer key (given by google)
 *
 * @param address the address to find
 * @param ssl defines if ssl should be used
 * @return the GoogleGeoCode found
 * @throws Exception in case of any error
 *
 */
public GoogleGeoCode getGeoCode(String address, boolean ssl) throws Exception {
    // build url
    StringBuilder url = new StringBuilder("http");
    if ( ssl ) {
        url.append("s");
    }
  
    url.append("://maps.googleapis.com/maps/api/geocode/json?");
  
    if ( ssl ) {
        url.append("key=");
        url.append(API_KEY);
        url.append("&");
    }
    url.append("sensor=false&address=");
    url.append( URLEncoder.encode(address) );
  
    // request url like: http://maps.googleapis.com/maps/api/geocode/json?address=" + URLEncoder.encode(address) + "&sensor=false"
    // do request
    try (CloseableHttpClient httpclient = HttpClients.createDefault();) {
        HttpGet request = new HttpGet(url.toString());

        // set common headers (may useless)
        request.setHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.6.0");
        request.setHeader("Host", "maps.googleapis.com");
        request.setHeader("Connection", "keep-alive");
        request.setHeader("Accept-Language", "en-US,en;q=0.5");
        request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        request.setHeader("Accept-Encoding", "gzip, deflate");

        try (CloseableHttpResponse response = httpclient.execute(request)) {
            HttpEntity entity = response.getEntity();

            // recover String response (for debug purposes)
            StringBuilder result = new StringBuilder();
            try (BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()))) {
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    result.append(inputLine);
                    result.append("\n");
                }
            }

            // parse result
            ObjectMapper mapper = new ObjectMapper();
            GoogleGeoCode geocode = mapper.readValue(result.toString(), GoogleGeoCode.class);

            if (!"OK".equals(geocode.getStatus())) {
                if (geocode.getError_message() != null) {
                    throw new Exception(geocode.getError_message());
                }
                throw new Exception("Can not find geocode for: " + address);
            }
            return geocode;
        }
    }
}



HttpComponents and Jackson Parser made the job easier.

You may notice some request returns most than 1 result, in this case, what about for keeping the better one?

I define the better one as the result with the most similar address to the one requested (google gives you a formatted address for every result),

I used a simple approach for measuring this, the Longest Common Subsequence, the next method will help you to filter the results into the best one, I used in my app and seems to work really well.

/**
 * Given an address and google geocode find the most probable location of
 * address, the measure uses the longest common subsequence algorithm and a
 * minimum requirement for similarity
 *
 * @param address the original address
 * @param geocode the google geocode results
 * @return the most probable location (lat, lng), null if no one matches
 */
public GoogleGeoLatLng getMostProbableLocation(String address, GoogleGeoCode geocode) {
    address = address.toLowerCase();
    int expected = address.length() / 2;
    int sz = geocode.getResults().length;
    int best = expected;
    GoogleGeoLatLng latlng = null;
    for (GoogleGeoResult result : geocode.getResults()) {
        GoogleGeoLatLng cur = result.getGeometry().getLocation();
        String formattedAddress = result.getFormatted_address().toLowerCase();
        int p = lcs(address, formattedAddress);

        if (p > best) {
            latlng = cur;
            best = p;
        }
    }
    return latlng;
}




And the LCS method:

/**
 * The longest common subsequence of s and t using dynamic programming
 *
 * @param s the first string
 * @param t the second string
 * @return the length of the longest common subsequence
 */
private int lcs(String s, String t) {
    int N = s.length();
    int M = t.length();
    int[][] ans = new int[N + 1][M + 1];
    for (int k = N - 1; k >= 0; k--) {
        for (int m = M - 1; m >= 0; m--) {
            if (s.charAt(k) == t.charAt(m)) {
                ans[k][m] = 1 + ans[k + 1][m + 1];
            } else {
                ans[k][m] = Math.max(ans[k + 1][m], ans[k][m + 1]);
            }
        }
    }
    return ans[0][0];
}



I packet them into a simple class:

/**
 * Utils for Google geocoding api
 * 
 * @author Alexis Hernandez
 */
public class GoogleGeoUtils {
 public GoogleGeoCode getGeoCode(String address, boolean ssl); 
 public GoogleGeoLatLng getMostProbableLocation(String address, GoogleGeoCode geocode);
 private int lcs(String s, String t);
}


I may attach the sources later.

IMPORTANT NOTE: I used HttpClient 4.5 (the current latest version) but some previous versions have issues requesting google apis using SSL.


After I wrote the code I found a library which appears to do the work: https://code.google.com/p/geocoder-java/

If you want to, give it a try, I didn't tested.


I hope it can be useful for you, thanks for reading and see you in the next post.

Saturday, July 4, 2015

Adafruitt FONA MiniGSM C Library for Raspberry Pi and similars

Adafruit FONA MiniGSM is a great product, you can learn more here, they wrote a library for Arduino which is found here.

If you buy this product and use it on Arduino, you will have not problems but what about if you need to do some complex needing a Raspberry Pi (RPi) or similar computer? Adafruit provide you a post for connecting FONA to a RPi here but has nothing for programming something.

When I bought FONA (like a year ago) I was in this situation, I wrote a small library in C which supports the most basic operations like send command and check command reply, I wrote a function to send SMS which basically use the previous functions, if you need more functions is not hard to extend the library.

The library is written in C and tested in Linux, it should work with most Linux distributions and with most SIMXXX chips working with AT commands.

I put a Main function as an example for using this library.

Note: This assume you have connected FONA to the port "/dev/ttyUSB0", be sure to change it if is not the case.

/**
 * source: http://stackoverflow.com/a/6947758/3211175
 *
 * Author: Alexis Hernandez
 *
 * gcc adafruit_fona_rpi.c -o fona -std=c99
**/

#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>

#include <signal.h>
#include <fcntl.h>
#include <features.h>
#include <string.h>

#ifndef CRTSCTS
#  ifdef CNEW_RTSCTS
#    define CRTSCTS CNEW_RTSCTS
#  else
#    define CRTSCTS 0x80000000
#  endif /* CNEW_RTSCTS */
#endif /* !CRTSCTS */

int set_interface_attribs(int fd, int speed, int parity)    {
        struct termios tty;
        memset(&tty, 0, sizeof tty);
        if ( tcgetattr(fd, &tty) !=  0 )    {
                printf("error %d from tcgetattr\n", errno);
                return -1;
        }

        cfsetospeed (&tty, speed);
        cfsetispeed (&tty, speed);

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as \000 chars
        tty.c_iflag &= ~IGNBRK;         // disable break processing
        tty.c_lflag = 0;                // no signaling chars, no echo,
                                        // no canonical processing
        tty.c_oflag = 0;                // no remapping, no delays
        tty.c_cc[VMIN]  = 0;            // read doesn't block
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                        // enable reading
        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        if ( tcsetattr(fd, TCSANOW, &tty) != 0 )    {
                printf("error %d from tcsetattr", errno);
                return -1;
        }
        return 0;
}

void set_blocking(int fd, int should_block)    {
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if ( tcgetattr(fd, &tty) != 0 )    {
                printf("error %d from tggetattr", errno);
                return;
        }

        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        if ( tcsetattr(fd, TCSANOW, &tty) != 0 )    {
                printf("error %d setting term attributes", errno);
        }
}


int fd;    // file descriptor for connection
char buf [100];    // buffer for the reply

// init connection
int startConnection(char *portname)    {

    fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0)    {
        printf("error %d opening %s: %s", errno, portname, strerror (errno));
        return 0;
    }

    set_interface_attribs(fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
    set_blocking(fd, 0);                // set no blocking
   
    //
    send("AT");
    usleep(100000);
   
    // turn off Echo!j
    send("ATE0");
    usleep(100000);
   
    return    sendCheckReply("ATE0", "OK");
}

void endConnection()    {
    close(fd);
}


// send a command to the serial port, read the answer but do nothing
int send(char *cmd)    {
//    printf("send: %s\n", cmd);
   
    int cmd_len = strlen(cmd);
    write( fd, cmd, cmd_len );                // send cmd
    write( fd, "\n", 1 );

    usleep ( (cmd_len + 10 + 25) * 300 );        // sleep enough to transmit the cmd plus
                                        // receive 25:  approx 100 uS per char
                                       
    int read_len = read(fd, buf, sizeof buf);  // read up to 100 characters if ready to read
   
    /*
    // print result
    printf(" read: %d bytes\n", read_len);
    for (int i = 0; i < read_len; i++)    printf( " %c", buf[i] );
    printf("\n");
    for (int i = 0; i < read_len; i++)    printf( " %d", buf[i] );
    printf("\n");
    */
   
    return 1;
}


/**
 * send cmd and checks device's reply match exact message
 * cmd and reply should ends with "cr+lf"
**/
int sendCheckReply(char *cmd, char *reply)    {

//    printf("sendCheckReply: %s\n", cmd);
    int cmd_len = strlen(cmd);
    write( fd, cmd, cmd_len );                // send cmd
    write( fd, "\n", 1 );

    usleep ( (cmd_len + 10 + 25) * 300 );        // sleep enough to transmit the cmd plus
                                        // receive 25:  approx 100 uS per char
                                       
    int read_len = read(fd, buf, sizeof buf);  // read up to 100 characters if ready to read
    // should return some like "\r\nxx\r\n", then avoid first 2 and last 2 bytes
   
    // print result
    /*
    printf(" read: %d bytes\n", read_len);
    for (int i = 0; i < read_len; i++)    printf( " %c", buf[i] );
    printf("\n");
    for (int i = 0; i < read_len; i++)    printf( " %d", buf[i] );
    printf("\n");
    */
   
    // strcmp
    int reply_idx = 0;
    int reply_len = strlen(reply);
    int read_idx = 2;
    read_len -= 2;
   
    for (; reply_idx < reply_len && read_idx < read_len && reply[reply_idx] == buf[read_idx]; reply_idx++, read_idx++);
   
    return    reply_idx == reply_len && read_idx == read_len ? 1 : 0;
}



/**
 * send cmd and checks device's reply is a prefix of response
**/
int sendCheckReplyPrefix(char *cmd, char *reply, int extraSleep)    {

    printf("sendCheckReplyPrefix: %s\n", cmd);
    int cmd_len = strlen(cmd);
    write( fd, cmd, cmd_len );                // send cmd
    write( fd, "\n", 1 );

    usleep ( (cmd_len + 10 + 25) * 300 );        // sleep enough to transmit the cmd plus
                                        // receive 25:  approx 100 uS per char
              
    if    ( extraSleep )
        usleep(extraSleep);
                       
    int read_len = read(fd, buf, sizeof buf);  // read up to 100 characters if ready to read
    // should return some like "\r\nxx\r\n", then avoid first 2 and last 2 bytes
   
    // print result
    printf(" read: %d bytes\n", read_len);
    for (int i = 0; i < read_len; i++)    printf( " %c", buf[i] );
    printf("\n");
    for (int i = 0; i < read_len; i++)    printf( " %d", buf[i] );
    printf("\n");
   
    // strcmp
    int reply_idx = 0;
    int reply_len = strlen(reply);
    int read_idx = 2;
   
    for (; reply_idx < reply_len && read_idx < read_len && reply[reply_idx] == buf[read_idx]; reply_idx++, read_idx++);
   
    return    reply_idx == reply_len ? 1 : 0;
}


// send smmmsg to smsaddr
int sendSMS(char *smsaddr, char *smsmsg) {
    if ( !sendCheckReply("AT+CMGF=1", "OK") )    {
        printf("fail: AT+CMGF=1\n");
        return 0;
    }
   
    // build send command like = AT+CMGS="nnnn"
    char sendcmd[30] = "AT+CMGS=\"";
    strncpy( sendcmd+9, smsaddr, 30-9-2); // 9 bytes beginning, 2 bytes for close quote + null
    sendcmd[ strlen(sendcmd) ] = '\"';
   
    printf("trying to send: %s\n", sendcmd);
   
    if ( !sendCheckReplyPrefix( &sendcmd[0], "> ", 0 ) )    {
        printf("fail: AT+CMGS=num\n");
        return 0;
    }
   
    // build msg command, append new line + ctrl+z
    char msgcmd [200];
    int msglen = strlen(smsmsg);
    strncpy(msgcmd, smsmsg, msglen);
    msgcmd[msglen] = '\n';
    msgcmd[msglen + 1] = 0x1A;
   
    return    sendCheckReplyPrefix( msgcmd, "+CMGS", 1000000 * 3 );
}








// main
int main()    {

    char *portname = "/dev/ttyUSB0";
   
    int attemps = 10;
    while    ( attemps-- &&    !startConnection(portname) )    usleep(1000);
   
    if    ( attemps <= 0 )    {
        printf( "ERROR: Can not start connection\n" );
        return    0;
    }
   
    printf("Connection started!\n");
   
    char *num = "0123456789";
    char *msg = "message here";
   
    if    ( sendSMS(num, msg) )    {
        printf("SMS sent\n");
    }    else    {
        printf("SMS can not be send\n");
    }
   
   
    printf("end\n");
   
    endConnection();
    return    0;
}






I hope this can be useful for you, be sure to ask any question.

Thanks for reading and see you in next post.

Thursday, July 2, 2015

Spring Security 4.x CSRF Protection for Facelets

Spring Security 4.x has enabled CSRF protection by default, this is great in most cases, the token should be sent in every form submission, if you are using Spring MVC you will not have problems cause the token is included automatically in every form (if protection is enabled).

Using JavaServer Pages (without spring mvc) needs spring security taglibs in order to include the toke (manually), example for meta tag:
<sec:csrfMetaTags>

Using Facelets you will get in trouble, if you search, there is a way to integrate Spring Security with Facelets with the taglibs ported for Facelets but the csrf tags are not included, I wrote a taglib including tha above one for Facelets, I gonna describe the way to integrate it (is really easy).

1.- Create a file for the taglib definition, in my case "/WEB-INF/taglibs/alex.springsecurity.taglib.xml"

<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd">

<facelet-taglib xmlns="http://java.sun.com/xml/ns/javaee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
                version="2.0">
    <namespace>http://www.huizaches.com/alex/spring-security-taglib</namespace>
    <tag>
        <tag-name>csrfMetaTags</tag-name>
        <component>
            <component-type>CsrfMetaTag</component-type>
        </component>
    </tag>
      
</facelet-taglib>


2.- Include the taglib as a context param in web.xml
 
    <!-- Spring Security Facelets Tag Library -->
    <context-param>
        <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
        <param-value>/WEB-INF/taglibs/alex.springsecurity.taglib.xml</param-value>
    </context-param>


3.- Create the source for handling the taglib (com.alex.spring.security.tags.CsrfMetaTag in my case)
 
package com.alex.spring.security.tags;

import java.io.IOException;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.springframework.security.web.csrf.CsrfToken;


@FacesComponent(value = "CsrfMetaTag")
public class CsrfMetaTag extends UIComponentBase {

    @Override
    public String getFamily() {
        return "csrfMetaTags";
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        CsrfToken token = (CsrfToken) req.getAttribute(CsrfToken.class.getName());
        if (token != null) {
            try {
                String s = handleToken(token);
                context.getResponseWriter().write(s);
            } catch (IOException e) {
                throw e;
            }
        }

    }

    public String handleToken(CsrfToken token) {
        return "<meta name=\"_csrf_parameter\" content=\"" + token.getParameterName()
                + "\" />" + "<meta name=\"_csrf_header\" content=\""
                + token.getHeaderName() + "\" />" + "<meta name=\"_csrf\" content=\""
                + token.getToken() + "\" />";
    }

}
 


4.- Include the namespace in your web file
 
      xmlns:csec="http://www.huizaches.com/alex/spring-security-taglib" 

5.- Put the taglib in the header (this is most for ajax requests)
 
      <csec:csrfMetaTags />
 
6.- Put this code inside every form you use.
      <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />


That's all, now every request should be working.
 
I hope this can be useful, thanks for reading and see you in the next post.
 

Simple Java XML Viewer

XML Files are very popular for storing and sharing information, I'll not repeat what you can find in search engines.

XML format is a tree structure for storing data and fits perfectly in a JTree.

I used dom4j for parsing the file (which is really easy to use).

This is a snapshot with a pom.xml used by maven:


















Dom4j provide a really easy way for parsing the file:

   SAXReader reader = new SAXReader();
   Document doc = reader.read(inputFile);

   Element e = doc.getRootElement();

Now you can traverse the xml tree using the root element in your favorite order.

You can see a way using recursion in the source code (attached into the jar file).

Download.

This can be helpful for parsing web pages, most of them are malformed but you can find a way to add / remove / change content with a little bit of parsing to make a well formed document and use dom4j to parse it for you (I did it several times).

I hope this can be helpful for you.

Thanks for reading and see you in the next post.

Wednesday, July 1, 2015

introduction

This is my first post and I want to introduce myself, my name is Alex, I'm an engineer living in Mexico.

I love programming, its not only about solve problems or automatize things to make the life easier but to order a dummy machine to make whatever you can think (and code of course).

In this blog I gonna write most about programming projects, rarely about life experiences but may in the future (who knows?).

I can't remember the day when I began to try programming, it was before 2008 summers, starting with assembler for ARM devices and a little bit for Intel x86, I learned most by myself using google and applying reverse engineering to small codes and modifying them, testing codes in my hacked cellphone, thats cause it lots and lots of crashes each day (mistakes in compiled code).

Now in 2015 I had done many different kind of programs, I gonna list some of them:
  • A simple calculator with GUI in MASM32 (I think the exe and source should be in my old Pentium 3 machine).
  • Parsers.
  • Web crawlers.
  • Karaoke player (music, karaoke using cdg format and videos) which I and some friends tried to commercialize (we got a little money and receive no complaints not bugs).
  • Cryptographic tools (I love cryptography).
  • Tools for Raspberry PI and Arduino.
  • A mini programming language called "Javita" based most in java.
  • Web Applications.
  • Some scripts.
  • Lots of algorithms and data structures implementations.
  • Lots more I can't remember.
Most of the work was done in java (which is the high-level programming language I use better).

I gonna try to find my projects and post them here, It may be helpful for you or some newbies.

Today I'm working for a big company programming in C (really cool)

I gonna end this post with a funny code comment I read today:
/*
Dear maintainer!
When I wrote this code, only I and God knew what it was.
Now, only God knows.

So, if you are done trying to 'optimize' this routine (and failed!).
Please increment the following counter as a warning to the next guy.

totalHoursWasterHere = 71
*/

I hope you enjoy it as I enjoy writing it.

Thanks for reading.