miércoles, 23 de diciembre de 2015

Políticas de Seguridad con JSON - JSONP

A veces no es posible hacer una llamada a Ajax a través de dominios distintos para traernos algún dato. Esto es una limitación en la programación web del lado del cliente y es debido a una restricción de seguridad de los navegadores. Esto es debido a una política de seguridad llamada: "Same-origin policy", pensada para evitar que se carguen mediante Javascript datos potencialmente inseguros, por estar en otros servidores.
Esta restricción se puede evitar si el dominio con el que te conectas está configurado para aceptar conexiones desde otros dominios. Esto se consigue activando la configuración "Cross-origin resource sharing", pero también puede solucionarse, independientemente de la configuración del servidor, a través del uso de JSONP.


Por qué JSONP puede accederlo

Para hacer posible el JSONP en situaciones cross-domain se usa una alternativa permitida de carga de scripts en una página. Como sabes, el navegador sí que acepta la carga de código Javascript que traigas con la etiqueta SCRIPT y el atributo src de otro dominio. Es la base de los servicios CDN.
<script src="http://example.com/alternativo/dominio"></script>
Debido a que eso no provoca ningún problema de seguridad, JSONP se aprovecha de ello para acceder de ese modo al recurso solicitado, inyectando ese script en la página y ejecutando su código para extraer el dato.

Nota: Si te llegase un JSON normal no podrías ejecutarlo tal cual. El JSON es un literal de objeto y no una sentencia Javascript que sea capaz de ejecutar el navegador, por ello no podrías meterlo tal cual en una etiqueta SCRIPT de la página.



jueves, 23 de abril de 2015

Diseño UX/UI

Desde el momento en que te inicias en diseño gráfico, empezamos a copiar y mejorar ideas. Inicialmente todo es maravilloso para ti, malo o bueno tu diseño te das cuenta que deseas mejorar la calidad de tu trabajo, que software es mejor conceptos de diseño, recomendaciones y hasta estándares.
El diseño gráfico es algo sencillo, no necesariamente es por computador y recientemente he visto que dejar el lápiz y papel para muchos ha sido error (me incluyo). Yo solía dibujar muy bien algo que nace de la curiosidad de cada uno de nosotros, cuando conoces lo que es luz y sombra todo nuestro trabajo se ilumina y empiezas a tener la impresión que te conviertes en un experto.
Equivocado, apenas encontramos la puerta de un mundo que nos espera.
Luego vez trabajos gráficos hechos por computador y decides probar que tal te va, buscas un software (cualquiera, normalmente comercial) y se inicia una fase de vectores, colores, líneas y sombras. tomas un producto comercial y empiezas a rehacer el logo para ver como aplicar las herramientas, al cabo de un tiempo ya haces trabajos que calan con cierto nivel profesional, pero le falta algo.
Empiezas a estudiar conceptos y aparecen nuevas formas de ver el diseño, vanguardia, tradicional, abstracto, comunicacional, comercial etc.
Te encuentras en el dilema de lo que te gusta y lo que mejor pagan, por que te parece que puedes vivir profesionalmente de lo que te gusta hacer, evidentemente el camino más corto es el comercial y vamos con el tiempo ya eres un experto en Photoshop, Illustrator, InDesign, sí en el mejor de los casos para convertirse en un profesional debes aprender a manejar esos programas, no cabe la menor duda.
Existen muchas otras herramientas con las que podemos hacer el mismo trabajo o resultado Gimp, Inkscape, Scribus pero en este caso los dejare para el final.
Como diseñador Gráfico te vas a encontrar con muchos, pero muchos candidatos en esa posición, actualmente alguien con unos 4-5 años de manejo de esos programas puede manejar muy bien la herramienta y es que no estoy hablando de creatividad ya que es otra cosa, puedes encontrar a licenciados en diseño pero que son muy creativos con búsqueda de imágenes en Google.
Los salarios de Diseñadores normalmente no son buenos -en términos generales- muy pocos logran llegar a convertirse en verdaderos “Senior” del diseño, deben combinar varios aspectos, Estudios -si no estudias diseño, nadie te considera diseñador-, Creatividad -se nace con ello- y me parece que Suerte. Evidentemente el Inglés no debe faltar en tu CV.
El tema es que los Diseñadores Gráficos no quieren ver un párrafo de más de 4 líneas y no se imaginan para nada tener que ver un código de algún lenguaje de programación, a un Diseñador Gráfico le llama más la atención un piercing, fumar cigarrillo a cada rato, hablar de la última campaña de J. Walter Thompson entre otros.
Desde hace muchos años algunos desarrolladores  de software les ha tocado hacer de diseñadores y poco a poco algunos diseñadores que se inclinan más a lo autodidacta, empiezan a tomar en cuenta la falencia de alguien que conozca algo de lenguajes de programación y que le guste el diseño y brilla en el horizonte la frase “Analista de Usabilidad” y es que muchos diseñadores autodidactas no saben que es una profesión que puede ser mejor pagada que la de un diseñador gráfico común y mortal.
Empezar a entender parte del lenguaje es necesario pero no es nada complejo, para los diseñadores solo basta con conocer un 5% de lo que los programadores escriben y con nuevos ambientes SDK hasta con menos conocimiento del lenguaje es suficiente.
Diseñar iconos en Photoshop o Illustrator no tiene absolutamente nada que envidiarle a hacerlo con Inkscape o Gimp, pero es indispensable conocer los software comerciales, deben -sí o sí- estar en tu CV, las empresas los toman en cuenta a primera vista, luego que tu decidas hacer tus trabajos con Gimp e Inkscape ellos se van a dar cuenta que no hay diferencia en el producto.
Se debe aprender con el tiempo estándares y lenguajes cono HTML5, CSS3, JavaScript y jQuery (jQuery), SVG aún cuando puede que no sea necesario del todo.
Para aprender que es UX/UI no solo es un tema de iconos, ventanas, colores, animaciones bonitas. Ahora mismo hay muchas tecnologías que vienen a relucir, con la salida al mercado del tema de Tablets, Android, iPhone etc. Los UX/UI están siendo muy cotizados ya que incluso cosas no tan nuevas como “Resposive Web Design” viene a ser tan importantes para el refrescamiento de sitios web.
Entre Experiencia de Usuario “UX” y el diseño de Interfaz de Usuario “UI”, no solo son conceptos de diseño, colores, facilidad. Representan también la personalidad del producto, la marca y confianza que puede generar al usuario. Lo que se en realidad no hay todavía una definición precisa, ya que es un Diseñador Gráfico con sólidos conceptos de experiencia con los usuarios y las interfaces de las aplicaciones.
Existen muchos puntos a tomar en cuenta, desde el tamaño de un icono, el color y minimalista de los diseños, la ayuda de los diálogos, logran que el usuario a primera vista sepa manejar el programa, los gráficos no deben ser confusos o estar fuera de la realidad, cuando una persona vea un botón el mismo debe hacer exactamente lo que como imagen expresa.
El UX cada día va cambiando y mejorando, por eso vemos que cada vez que un niño toma un iPad, iPhone etc. sabe usarlo casi que de inmediato y eso es gracias a UX.
Con el tema de UI simplemente es el diseño de interfaz de usuario, el teclado de una laptop, una calculadora, el volante y pedales de un carro, entre otros. Son la sinapsis entre un individuo y la máquina, detrás de todo ello está la UX y lo que hace en sí el programa.
Por cierto cada día salen más ofertas de empleo para los UX/UI, bienvenidos.
Xavier

lunes, 20 de abril de 2015

JAVA - Guía práctica para trabajar los niveles de isolation de las transacciones en ORM-JAVA

Introducción

A relational database strong consistency model is based on ACID transaction properties. In this post we are going to unravel the reasons behind using different transaction isolation levels and various configuration patterns for both resource local and JTA transactions.

Isolation and consistency

In a relational database system, atomicity and durability are strict properties, while consistency and isolation are more or less configurable. We cannot even separate consistency from isolation as these two properties are always related.
The lower the isolation level, the less consistent the system will get. From the least to the most consistent, there are four isolation levels:
  • READ UNCOMMITTED
  • READ COMMITTED (protecting against dirty reads)
  • REPEATABLE READ (protecting against dirty and non-repeatable reads)
  • SERIALIZABLE (protecting against dirty, non-repeatable reads and phantom reads)
Although the most consistent SERIALIZABLE isolation level would be the safest choice, most databases default to READ COMMITTED instead. According toAmdahl’s law, to accommodate more concurrent transactions, we have to reduce the serial fraction of our data processing. The shorter the lock acquisition interval, the more requests a database can process.

Isolation levels

As we previously demonstrated, application level repeatable reads paired with an optimistic locking mechanism are very convenient for preventing lost updates in long conversations.
In a highly concurrent environment, optimistic locking might lead to a high transaction failure rate. Pessimistic locking, like any other queuing mechanism might accommodate more transactions when giving a sufficient lock acquisition time interval.

Database and isolation levels

Apart from MySQL (which uses REPEATABLE_READ), the default isolation level of most relational database systems is READ_COMMITTED. All databases allow you to set the default transaction isolation level.
Typically, the database is shared among multiple applications and each one has its own specific transaction requirements. For most transactions the READ_COMMITTED isolation level is the best choice and we should only override it for specific business cases.
This strategy proves to be the very efficient, allowing us to have stricter isolation levels for just a subset of all SQL transactions.

DataSource isolation level

The JDBC Connection object allows us to set the isolation level for all transactions issued on that specific connection. Establishing a new database connection is a resource consuming process, so most applications use a connection poolingDataSource. The connection pooling DataSource can also set the default transaction isolation level:
Compared to the global database isolation level setting, the DataSource level transaction isolation configurations are more convenient. Each application may set its own specific concurrency control level.
We can even define multiple DataSources, each one with a per-defined isolation level. This way we can dynamically choose a specific isolation level JDBC Connection.

Hibernate isolation level

Because it has to support both resource local and JTA transactions, Hibernate offers a very flexible connection provider mechanism.
JTA transactions require an XAConnection and it’s the JTA transaction managerresponsibility to provide XA compliant connections.
Resource local transactions can use a resource local DataSource and for this scenario, Hibernate offers multiple connection provider options:
  • Driver Manager Connection Provider (doesn’t pool connections and therefore it’s only meant for simple testing scenarios)
  • C3P0 Connection Provider (delegating connection acquiring calls to an internal C3P0 connection pooling DataSource)
  • DataSource Connection Provider (delegating connection acquiring calls to an external DataSource.
Hibernate offers a transaction isolation level confgiuartion calledhibernate.connection.isolation, so we are going to check how all the aforementioned connection providers behave when being given this particular setting.
For this we are going to:
  1. Create a SessionFactory
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    @Override
    protected SessionFactory newSessionFactory() {
        Properties properties = getProperties();
     
        return new Configuration()
                .addProperties(properties)
                .addAnnotatedClass(SecurityId.class)
                .buildSessionFactory(
                        new StandardServiceRegistryBuilder()
                                .applySettings(properties)
                                .build()
        );
    }
  2. Open a new Session and test the associated connection transaction isolation level
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    @Test
        public void test() {
            Session session = null;
            Transaction txn = null;
            try {
                session = getSessionFactory().openSession();
                txn = session.beginTransaction();
                session.doWork(new Work() {
                    @Override
                    public void execute(Connection connection) throws SQLException {
                        LOGGER.debug("Transaction isolation level is {}", Environment.isolationLevelToString(connection.getTransactionIsolation()));
                    }
                });
                txn.commit();
            } catch (RuntimeException e) {
                if ( txn != null && txn.isActive() ) txn.rollback();
                throw e;
            } finally {
                if (session != null) {
                    session.close();
                }
            }
        }
The only thing that differs is the connection provider configuration.

Driver Manager Connection Provider

The Driver Manager Connection Provider offers a rudimentary DataSource wrapper for the configured database driver. You should only use it for test scenarios, since it doesn’t offer a professional connection pooling mechanism.
1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
protected Properties getProperties() {
    Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
        //driver settings
        properties.put("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
        properties.put("hibernate.connection.url", "jdbc:hsqldb:mem:test");
        properties.put("hibernate.connection.username", "sa");
        properties.put("hibernate.connection.password", "");
        //isolation level
        properties.setProperty("hibernate.connection.isolation", String.valueOf(Connection.TRANSACTION_SERIALIZABLE));
    return properties;
}
The test generates the following output:
1
2
WARN  [main]: o.h.e.j.c.i.DriverManagerConnectionProviderImpl - HHH000402: Using Hibernate built-in connection pool (not for production use!)
DEBUG [main]: c.v.h.m.l.t.TransactionIsolationDriverConnectionProviderTest - Transaction isolation level is SERIALIZABLE
The Hibernate Session associated JDBC Connection is using the SERIALIZABLE transaction isolation level, so the hibernate.connection.isolation configuration works for this specific connection provider.

C3P0 Connection Provider

Hibernate also offers a build-in C3P0 Connection Provider. Like in the previous example, we only need to provide the driver configuration settings and Hibernate instantiate the C3P0 connection pool on our behalf.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Override
protected Properties getProperties() {
    Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
        //log settings
        properties.put("hibernate.hbm2ddl.auto", "update");
        properties.put("hibernate.show_sql", "true");
        //driver settings
        properties.put("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
        properties.put("hibernate.connection.url", "jdbc:hsqldb:mem:test");
        properties.put("hibernate.connection.username", "sa");
        properties.put("hibernate.connection.password", "");
        //c3p0 settings
        properties.put("hibernate.c3p0.min_size", 1);
        properties.put("hibernate.c3p0.max_size", 5);
        //isolation level
        properties.setProperty("hibernate.connection.isolation", String.valueOf(Connection.TRANSACTION_SERIALIZABLE));
    return properties;
}
The test generates the following output:
1
2
3
4
5
Dec 19, 2014 11:02:56 PM com.mchange.v2.log.MLog <clinit>
INFO: MLog clients using java 1.4+ standard logging.
Dec 19, 2014 11:02:56 PM com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.2.1 [built 20-March-2013 10:47:27 +0000; debug? true; trace: 10]
DEBUG [main]: c.v.h.m.l.t.TransactionIsolationInternalC3P0ConnectionProviderTest - Transaction isolation level is SERIALIZABLE
So, the hibernate.connection.isolation configuration works for the internal C3P0 connection provider too.

DataSource Connection Provider

Hibernate doesn’t force you to use a specific connection provider mechanism. You can simply supply a DataSource and Hibernate will use it whenever a new Connection is being requested. This time we’ll create a full-blown DataSource object and pass it through the hibernate.connection.datasource configuration.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Override
protected Properties getProperties() {
    Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
        //log settings
        properties.put("hibernate.hbm2ddl.auto", "update");
        //data source settings
        properties.put("hibernate.connection.datasource", newDataSource());
        //isolation level
        properties.setProperty("hibernate.connection.isolation", String.valueOf(Connection.TRANSACTION_SERIALIZABLE));
    return properties;
}
 
protected ProxyDataSource newDataSource() {
        JDBCDataSource actualDataSource = new JDBCDataSource();
        actualDataSource.setUrl("jdbc:hsqldb:mem:test");
        actualDataSource.setUser("sa");
        actualDataSource.setPassword("");
        ProxyDataSource proxyDataSource = new ProxyDataSource();
        proxyDataSource.setDataSource(actualDataSource);
        proxyDataSource.setListener(new SLF4JQueryLoggingListener());
        return proxyDataSource;
}   
The test generates the following output:
1
DEBUG [main]: c.v.h.m.l.t.TransactionIsolationExternalDataSourceConnectionProviderTest - Transaction isolation level is READ_COMMITTED
This time, the hibernate.connection.isolation doesn’t seem to be taken into consideration. Hibernate doesn’t override external DataSources, so this settings is useless in this scenario.
If you are using an external DataSource (e.g. maybe through JNDI), then you need to set the transaction isolation at the external DataSource level.
To fix our previous example, we just have to configure the external DataSource to use a specific isolation level:
1
2
3
4
5
6
7
8
9
10
11
12
13
protected ProxyDataSource newDataSource() {
    JDBCDataSource actualDataSource = new JDBCDataSource();
    actualDataSource.setUrl("jdbc:hsqldb:mem:test");
    actualDataSource.setUser("sa");
    actualDataSource.setPassword("");
    Properties properties = new Properties();
    properties.setProperty("hsqldb.tx_level", "SERIALIZABLE");
    actualDataSource.setProperties(properties);
    ProxyDataSource proxyDataSource = new ProxyDataSource();
    proxyDataSource.setDataSource(actualDataSource);
    proxyDataSource.setListener(new SLF4JQueryLoggingListener());
    return proxyDataSource;
}
Generating the following output:
1
DEBUG [main]: c.v.h.m.l.t.TransactionIsolationExternalDataSourceExternalconfgiurationConnectionProviderTest - Transaction isolation level is SERIALIZABLE

Java Enterprise transaction isolation support

Hibernate has a built-in Transaction API abstraction layer, isolating the data access layer from the transaction management topology (resource local or JTA). While we can develop an application using Hibernate transaction abstraction only, it’s much more common to delegate this responsibility to a middle-ware technology (JEE orSpring).

Java Enterprise Edition

JTA (Java Transaction API specification) defines how transactions should be managed by a JEE compliant application server. On the client side, we can demarcate the transaction boundaries using the TransactionAttribute annotation. While we have the option of choosing the right transaction propagation setting, we cannot do the same for the isolation level.
JTA doesn’t support transaction-scoped isolation levels and so we have to resort to vendor-specific configurations for providing an XA DataSource with a specific transaction isolation setting.

Spring

Spring @Transactional annotation is used for defining a transaction boundary. As opposed to JEE, this annotation allows us to configure:
  • isolation level
  • exception types rollback policy
  • propagation
  • read-only
  • timeout
As I will demonstrate later in this article, the isolation level settings is readily available for resource local transactions only. Because JTA doesn’t support transaction-scoped isolation levels, Spring offers theIsolationLevelDataSourceRouter to overcome this shortcoming when using application server JTA DataSources.
Because most DataSource implementations can only take a default transaction isolation level, we can have multiple such DataSources, each one serving connections for a specific transaction isolation level.
The logical transaction (e.g. @Transactional) isolation level setting is introspected by the IsolationLevelDataSourceRouter and the connection acquire request is therefore delegated to a specific DataSource implementation that can serve a JDBC Connection with the same transaction isolation level setting.
So, even in JTA environments, the transaction isolation router can offer a vendor-independent solution for overriding the default database isolation level on a per transaction basis.

Spring transaction-scoped isolation levels

Next, I’m going to test the Spring transaction management support for both resource local and JTA transactions.
For this, I’ll introduce a transactional business logic Service Bean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Service
public class StoreServiceImpl implements StoreService {
 
    protected final Logger LOGGER = LoggerFactory.getLogger(getClass());
 
    @PersistenceContext(unitName = "persistenceUnit")
    private EntityManager entityManager;
 
    @Override
    @Transactional(isolation = Isolation.SERIALIZABLE)
    public void purchase(Long productId) {       
        Session session = (Session) entityManager.getDelegate();
        session.doWork(new Work() {
            @Override
            public void execute(Connection connection) throws SQLException {
                LOGGER.debug("Transaction isolation level is {}", Environment.isolationLevelToString(connection.getTransactionIsolation()));
            }
        });
    }
}
The Spring framework offers a transaction management abstraction that decouples the application logic code from the underlying transaction specific configurations. The Spring transaction manager is only a facade to the actual resource local or JTA transaction managers.
Migrating from resource local to XA transactions is just a configuration detail, leaving the actual business logic code untouched. This wouldn’t be possible without the extra transaction management abstraction layer and the cross-cutting AOP support.
Next we are going to test how various specific transaction managers support transaction-scope isolation level overriding.

JPA transaction manager

First, we are going to test the JPA Transaction Manager:
1
2
3
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
When calling our business logic service, this is what we get:
1
DEBUG [main]: c.v.s.i.StoreServiceImpl - Transaction isolation level is SERIALIZABLE
The JPA transaction manager can take one DataSource only, so it can only issueresource local transactions. In such scenarios, Spring transaction manager is able to override the default DataSource isolation level (which is READ COMMITTED in our case).

JTA transaction manager

Now, let’s see what happens when we switch to JTA transactions. As I previously stated, Spring only offers a logical transaction manager, which means we also have to provide a physical JTA transaction manager.
Traditionally, it was the enterprise application server (e.g. WildflyWebLogic) responsibility to provide a JTA compliant transaction manager. Nowadays, there is also a great variety of stand-alone JTA transaction managers:
In this test, we are going to use Bitronix:
1
2
3
4
5
6
7
8
<bean id="jtaTransactionManager" factory-method="getTransactionManager"
      class="bitronix.tm.TransactionManagerServices" depends-on="btmConfig, dataSource"
      destroy-method="shutdown"/>
 
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager" ref="jtaTransactionManager"/>
    <property name="userTransaction" ref="jtaTransactionManager"/>
</bean>
When running the previous test, we get the following exception:
1
org.springframework.transaction.InvalidIsolationLevelException: JtaTransactionManager does not support custom isolation levels by default - switch 'allowCustomIsolationLevels' to 'true'
So, let’s enable the custom isolation level setting and rerun the test:
1
2
3
4
5
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager" ref="jtaTransactionManager"/>
    <property name="userTransaction" ref="jtaTransactionManager"/>
    <property name="allowCustomIsolationLevels" value="true"/>
</bean>
The test gives us the following output:
1
DEBUG [main]: c.v.s.i.StoreServiceImpl - Transaction isolation level is READ_COMMITTED
Even with this extra configuartion, the transaction-scoped isolation level wasn’t propagated to the underlying database connection, as this is the default JTA transaction manager behavior.
For WebLogic, Spring offers a WebLogicJtaTransactionManager to address this limitation, as we can see in the following Spring source-code snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Specify isolation level, if any, through corresponding WebLogic transaction property.
if (this.weblogicTransactionManagerAvailable) {
    if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
        try {
            Transaction tx = getTransactionManager().getTransaction();
            Integer isolationLevel = definition.getIsolationLevel();
            /*
            weblogic.transaction.Transaction wtx = (weblogic.transaction.Transaction) tx;
            wtx.setProperty(ISOLATION_LEVEL_KEY, isolationLevel);
            */
            this.setPropertyMethod.invoke(tx, ISOLATION_LEVEL_KEY, isolationLevel);
        }
        catch (InvocationTargetException ex) {
            throw new TransactionSystemException(
                    "WebLogic's Transaction.setProperty(String, Serializable) method failed", ex.getTargetException());
        }
        catch (Exception ex) {
            throw new TransactionSystemException(
                    "Could not invoke WebLogic's Transaction.setProperty(String, Serializable) method", ex);
        }
    }
}
else {
    applyIsolationLevel(txObject, definition.getIsolationLevel());
}


Fuente: http://vladmihalcea.com/2014/12/23/a-beginners-guide-to-transaction-isolation-levels-in-enterprise-java/