sábado, 31 de agosto de 2013

Spring Beans with JMX(Java Management Extension)

 - Spring Beans with JMX(Java Management Extension)  

     Technology that enables you to create elements of management, monitoring and configuration of applications.
     - Remoting MBeans
         JMX é a tecnologia(API) que permite implementar “management interfaces” ou seja gerenciar aplicações Java.  
         •    Abstrai a o trabalho de gerencia de informação para uma interface comum para facilitar o gerenciamento integrado.
         •    Distribui o gerenciamento movendo as funções de gerenciamento para os agentes.
         •    Deploy real-time de serviços e modificações
         •    Possibilidade de uso para gerenciar qualquer aplicação ou device ((via JNI or SNMP etc..)
 
     Key component-> MBean(Managed bean).
         MBean is a JavaBean that exposes certain methods which define the management interface.
     Four types of MBeans
         Standard MBeans: Management interface is determined by reflection on a fixed Java interface that is implemented by the bean class.
         Dynamic MBean: Management interface is determined at runtime by invoking methods of the DynamicMBean interface.
         Open MBeans: Special kind of dynamic MBean. attributes and operations are limited to private elements(types, class wrappers)
         Model MBean: Special kind of dynamic MBean. Bridges a management interface to the managed resource.
     Exporting Spring beans as MBeans
         Ex of changing a value of a attribute in a Controller
             class TestController {
                 ...
                 private int intValue = 10;
                 public void setIntValue(int value) {
                     intValue = value;
                 }
                 publid int getIntValue() { return intValue;}
             }
             TestController can be exposed as an MBean.
             <bean id="mbeanExporter"             <--- Is the bean that exports Spring-managed beans as Model MBeans in the MBeanServer(container)
                 class="org.springframework.jmx.export.MBeanExporter">
                 <property name="beans">
                     <map>
                         <entry key="beanTest:name=TestController" value-ref="testController"/>
                     </map>
                 </property>
                 <property name="server"ref="mbeanServer"/>
                 <property name="assembler"ref="assembler"/>        <---- Expose the methods
             </bean>
             - After this, JConsole or VisualVM can view the bean's properties and invoke their methods.
         - Exposing methods by name
             <bean id="assembler"  
                 class="org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler"
                 p:managedMethods="getSpittlesPerPage,setSpittlesPerPage"/>
             or  
             <bean id="assembler" class="org.springframework.jmx.export.assembler.MethodExclusionMBeanInfoAssembler"
                 p:ignoredMethods="showHomePage"/>
     - Beans with annotations
         In app-spring.xml
             <context:mbean-export server="mbeanServer"/>
             
             User @ManagedResource, @ManagedOperation, @ManagedAttribute
             
             @Controller
             @ManagedResource(objectName="testObject:name=TestController")//
             public class TestController{
                 ...
                 @ManagedAttribute//
                 public void setSpittlesPerPage(int spittlesPerPage){
                     this.spittlesPerPage=spittlesPerPage;
                 }
                 @ManagedAttribute//
                     public int getSpittlesPerPage(){
                         return spittlesPerPage;
                     }
             }
 
     - Mbean Server
         É o core do agente. Ele provê um registo para os Mbeans.
         Permite que os clientes descubram e executem operações expostar pelos Mbeans.
         Disponibiliza vários serviços para facilitar a gerência (ex: monitoramento, escalonamento, etc)
         Usa “ObjectName” class para registrar os objetos com o MBeanServer  
 
         Lida com os recursos como MBeans
         suporte Mbean para “managed bean”
         MBeans podem representar um device físico ou uma aplicação.
         Usuário decide quais atributos e métodos ele deseja expor para gerenciamento.
         Usa design patters similares aos JavaBeans
         MBeans são expostos em um agente.
 
     - Java Management Extensions Remote API
         To make MBeans available as remote objects:
             <bean class="org.springframework.jmx.support.ConnectorServerFactoryBean"/>
             Default: service:jmx:jmxmp://localhost:9875.
         There are other remoting protocol options rather than JMXMP, including RMI, SOAP, IIOP.
         To specify a diferent protocol:
             <bean class="org.springframework.jmx.support.ConnectorServerFactoryBean"
                     p:serviceUrl="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/spitter"/>
         Accessing remote MBeans
             JMX Remote API
                 Adiciona capacidade remota para a especificação JMX
                 Faz o agente JMX acessível fora da JVM.
                 Suporte padrão via RMI
                 Suporte opcional via TCP Sockets (JMXMP)
                 Suporta serviços de discovery/lookup e define segurança entre cliente e servidor
                 Assim como com código RMI, deve lidar com exceções de comunicação
             Configure MBeanServerConnectionFactoryBean in app-spring.xml
                 Factory to create MBeanServerConnection.
                 <bean id="mBeanServerClient"class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean"
                     p:serviceUrl="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/spitter"/>
             MBeanServerConnection
                 Local proxy to the remote MBean server.
                 <bean id="jmxClient"class="com.springinaction.jmx.JmxClient">
                     <property name="mbeanServerConnection"ref="mBeanServerClient"/>
                 </bean>
             Using the bean
                 int mbeanCount=mbeanServerConnection.getMBeanCount();
                 System.out.println("Thereare"+mbeanCount+"MBeans");
                 
                 mbeanServerConnection.invoke(new ObjectName("spitter:name=HomeController"),
                         "setSpittlesPerPage",
                         new Object[]{100},
                         new String[]{"int"});
     - Handging notifications
 

Spring Messaging - JMS

 - Messaging in Spring(JMS)  

     - Relating to asynchronous messaging
     - Use ActiveMQ (with maven)
     - create connection factory
         <bean id="connectionFactory"   <---- To send message through message broker.
                 class="org.apache.activemq.spring.ActiveMQConnectionFactory">
             <property name="brokerURL"value="tcp://localhost:61616"/>
         </bean>
         OR
         xmlns:amq="http://activemq.apache.org/schema/core"
                     xsi:schemaLocation="http://activemq.apache.org/schema/core
                     http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd
                 ...
         <amq:connectionFactoryid="connectionFactory"
             brokerURL="tcp://localhost:61616"/>
     - Declaring an ActiveMQ message destination
         Can be a queue
             <bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">
                 <constructor-argvalue="spitter.queue"/>                <------ Specific the name of the queue
             </bean>
             or
             <amq:queueid="queue"physicalName="spitter.queue"/>
         or a topic
             <bean id="topic"class="org.apache.activemq.command.ActiveMQTopic">
                 <constructor-argvalue="spitter.topic"/>                <------ Specific the name of the topic
             </bean>
             or  
             <amq:topicid="topic"physicalName="spitter.topic"/>
     - Using Spring JMS Template
         JmsTemplace is the center of the Spring JMS engine. Avoid the verbose and repetitive JMS code.
         It creates a connection, obtains a session, sends and receives messages.
         Wiring a JMS template
             <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
                 <property name="connectionFactory"ref="connectionFactory"/> <--- Reference to the connectionFactory
             </bean>
         Sending Messages
             - Create a interface with some information
                 public interface AlertService{
                     void sendSpittleAlert(Spittle spittle);
                 }
             - Create a implementation that uses JmsTemplate to send a object to a message queue
                 public class AlertServiceImpl implements AlertService{
                     public void sendSpittleAlert(final Spittle spittle){
                         jmsTemplate.send(            <--- Send Message
                             "spittle.alert.queue",        <---Destination
                             new MessageCreator(){
                                 public Message createMessage(Sessionsession) throws JMSException{
                                     return session.createObjectMessage(spittle);    <--- Create the message
                                 }
                             }
                         );
                     }
                     @Autowired
                     JmsTemplate jmsTemplate; <---- Inject JMS template
                 }
         - It is possible to define a default destination. Then it is not necessary to pass the destination when sending messages
             <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
                 <property name="connectionFactory"ref="connectionFactory"/>
                 <property name="defaultDestinationName" value="spittle.alert.queue"/>
             </bean>
             And just call
             jmsTemplate.send(
                 new MessageCreator(){
                     ...
                 }
             );
         - Consuming a Message
             public SpittlegetAlert(){
                 try {
                     ObjectMessage receivedMessage= (ObjectMessage) jmsTemplate.receive();  <--- Receive the message
                     return(Spittle)receivedMessage.getObject();    <--- Get the object
                 } catch(JMSExceptionjmsException){
                     throw JmsUtils.convertJmsAccessException(jmsException);
                 }
             }
     - Message Driven Bean using POJO
         @MessageDriven(mappedName="jms/spittle.alert.queue")  <---- Name of queue
         public class SpittleAlertHandler implements MessageListener{
             @Resource
             private MessageDrivenContext mdc;
             public void onMessage(Message message){
             ...
             }
         }
         - Using just POJO
             public classSpittleAlertHandler{
                 public voidprocessSpittle(Spittlespittle){
                     // ...implementationgoeshere...
                 }
             }
             Declaration
                 <bean id="spittleHandler" class="com.habuma.spitter.alerts.SpittleAlertHandler"/>
                 
                 <jms:listener-container connection-factory="connectionFactory">
                     <jms:listener destination="spitter.alert.queue" ref="spittleHandler"method="processSpittle"/>
                 </jms:listener-container>
 

Spring REST

 - Rest (Representational State Transfer)  

         Rest is resource-oriented. Emphasizes the things and nouns that describe an application.
         - Introduction
             Representational: Rest resources can be represented on virtually any form(XML, JSON, HTML, etc)
             State: We are more concerned with the state of a resource than with the actions we can take against resource
             Transfer: REST involves transferring resource data, in soem representational form between applications.
             In other words:
                 REST is about transferring the state of resources in any form from a server to a client.
             For this reason, we should say: RESTful resources.
         REST vs SOAP Web Services
             Rest:  
                 Lightweight – sem muito extra xml markupt    , Resultados lidos por humanos, Fácil para construir. Sem necessidade de toolkits.
                 A enfase é na comunicação ponto a ponto por HTTP
                 Mais baseada no design web-based. Possui falta de padrões de suporte a segurança, política, confiabilidade no envio de mensagem.
             Soap
                 Fácil consumo, rígido na checagem de tipo, lida com erros, possui ferramentas de desenvolvimento.
                 É um protocolo para computação distribuida XML-based.    
         Rest with Spring
             - Controllers can handle all HTTP methods: GET, PUT, DELETE, POSR
             - @PathVariable annotation enables controllers to handle requests for parametrized URLs
             - <form:form> with HiddenHttpMethodFilter, make it possible to submit PUT and DELETE from HTML forms
             - Resources can be representes in XML, JSON, Atom, RSS, etc
             - @ResponseBody annotation and other HttpMethodConverter implementation for rendering amd marshall
             - RestTemplate simplifies client-side consumption of REST resources.
         Writing Controllers
             - Restless
                 @Controller
                 @RequestMapping("/displaySpittle.htm")
                 public class DisplaySpittleController{
                     @RequestMapping(method=RequestMethod.GET)
                         public String showSpittle(@RequestParam("id")longid,Modelmodel){
                             model.addAttribute(spitterService.getSpittleById(id));
                             return"spittles/view";
                     }
                 }
                 Ex of call:
                     http://localhost:8080/Spitter/display/displaySpittle.htm?id=123
             - RESTFUL
                 - Principle: No two resource could share the same URL. The path is parameterized.
                 - No query parameters and tend to be hierarchical.
                 Ex of call:
                     http://localhost:8080/Spitter/splitters/123
                     @Controller
                     @RequestMapping("/spittles")
                     public class SpittleController{
                         private SpitterService spitterService;
                         @Inject
                         public SpittleController(SpitterService spitterService){
                             this.spitterService=spitterService;
                         }
                         @RequestMapping(value="/{id}", method=RequestMethod.GET)
                         public StringgetSpittle(@PathVariable("id")longid,    Model model){
                             model.addAttribute(spitterService.getSpittleById(id));
                             return"spittles/view";
                         }
                     }
             - Verbs
                 POST: Post data to the server to be handled by a processor. Usually create a new resource
                     @RequestMapping(method=RequestMethod.POST)
                     @ResponseStatus(HttpStatus.CREATED) //201= created
                     public @ResponseBody Spittle createSpittle(@Valid Spittle spittle,
                                     BindingResult result,HttpServletResponse response) throws BindException{
                         if(result.hasErrors()){
                             thrownewBindException(result);
                         }
                         spitterService.saveSpittle(spittle);
                         response.setHeader("Location","/spittles/"+spittle.getId()); <--- Set resource location  
                         return spittle;
                     }
                     - POST operates against a URL that doesn't exist yet, because the resource is new.
                 GET: Retrieve information(list, or just name of element)
                 PUT: Update and save a data
                     @RequestMapping(value="/{id}",method=RequestMethod.PUT)
                     @ResponseStatus(HttpStatus.NO_CONTENT)    //Status should be se to 204<---- To let the client know if the process was successful
                     public void putSpittle(@PathVariable("id")longid, @Valid Spittlespittle){
                         spitterService.saveSpittle(spittle);
                     }
                 DELETE: Delete a data
                     @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
                     @ResponseStatus(HttpStatus.NO_CONTENT)                    <---- To let the client know if the process was successful
                     public voiddeleteSpittle(@PathVariable("id")longid){
                         spitterService.deleteSpittle(id);
                     }
                 OPTIONS: Request avaiable options for communication with the server.
                 HEAD: Like Get, but a head should be returned
                 TRACE: Echoes the request body back to the client.
         - HTTP message converter
             @ResponseBody: Tells spring to return a object as a resource to client.
             - If use Jackson the object returned from the handler method will be given to the MappingJacksonHttpMessageConverter for conversion into JSON
             to the client.
         - Writing REST clients / RestTemplates
             Methods to interact with REST resouruces  
             Ex GET:
                 public Spittle[]retrieveSpittlesForSpitter(String username){
                     return new RestTemplate().getForObject(
                         "http://localhost:8080/Spitter/spitters/{spitter}/spittles", Spittle[].class,username); // {spitter} <--- username
                 }
                 or
                 public Spittle[]retrieveSpittlesForSpitter(Stringusername){
                     Map<String,String>urlVariables=newHashMap<String,String();
                     urlVariables.put("spitter",username);
                     return new RestTemplate().getForObject(
                         "http://localhost:8080/Spitter/spitters/{spitter}/spittles",Spittle[].class, urlVariables);
                 }
             PUT
                 public voidupdateSpittle(Spittlespittle) throws URISyntaxExceptione{
                     Stringurl="http://localhost:8080/Spitter/spittles/"+spittle.getId();
                     new RestTemplate().put(newURI(url),spittle);
                 }
                 OR
                 public voidupdateSpittle(Spittlespittle)throwsSpitterException{
                     restTemplate.put("http://localhost:8080/Spitter/spittles/{id}", spittle, spittle.getId());
                 }
             DELETE
                 public void deleteSpittle(longid){
                     restTemplate.delete("http://localhost:8080/Spitter/spittles/{id}",id));
                 }
             POST  
                 public Spitter postSpitterForObject(Spitterspitter){
                     RestTemplate rest = new RestTemplate();
                     return rest.postForObject("http://localhost:8080/Spitter/spitters",    spitter, Spitter.class);
                     }
                 }
                 Or
                 RestTemplate rest=new RestTemplate();
                 ResponseEntity<Spitter>response = rest.postForEntity("http://localhost:8080/Spitter/spitters",spitter,Spitter.class);
                 Spitter spitter=response.getBody();  <---- Body of Response returns Spitter
                 URI url=response.getHeaders().getLocation();
         - Writing <sf:form>
             To be used in PUT and DELETE
             <sf:form method="delete"modelAttribute="spitter">
                 ...
             </sf:form>
             result:
                 <form method="post">  
                     <input type="hidden"name="_method"value="delete"/>
                         ...
                 </form>

Spring Security

 - Spring Security  

 - Spring Security  
      Provide declarative security for Spring-based applications using filters.  
      Handle authentication and authorization from two angles  
           - web request level and restrict access at the URL  
           - method invocatino level using Spring AOP(proxying objects and applying advice that ensures that the user has authority to invoke a method)  
      - Install using Maven. Very easy  
      - Configuration  
           Basic configuration of spring-security.xml  
           <beans:beans xmlns="http://www.springframework.org/schema/security"  
                xmlns:beans="http://www.springframework.org/schema/beans"   
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
                xsi:schemaLocation="http://www.springframework.org/schema/beans  
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
                http://www.springframework.org/schema/security  
                http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">  
           </beans:beans>  
      - Securing web request  
           DelegatingFilterProxy(Servlet Context) --Delegates to --> Spring-Injected filter(Spring Application Context).  
           - Performed using filters  
                Spring security looks for a bean called springSecurityFilterChain  
                in web.xml  
                <filter>  
                     <filter-name>springSecurityFilterChain</filter-name>  <--- this class delegates to a filter resgistered as a bean  
                     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
                </filter>  
                <filter-mapping>  
                     <filter-name>springSecurityFilterChain</filter-name>  
                     <url-pattern>/*</url-pattern>  
                </filter-mapping>  
           - Configure app-spring.xml  
                - http://localhost:8080/Spitter/spring_security_login.  
                     Configure to Spring security intercept all URL (/**) and restrict access only to ROLE_TEST  
                     <http auto-config="true" >   
                          <form-login/>          |  
                          <http-basic/>          | <--- Uses a default login page.  
                          <logout/>               |  
                          <intercept-url pattern="/**"access="ROLE_TEST"/>  <---- interceptor to url  
                     </http>  
                     <context-param>  
                          <param-name>contextConfigLocation</param-name>  
                          <param-value>  
                               /WEB-INF/test-servlet.xml  
                               /WEB-INF/spring-security.xml  
                          </param-value>  
                     </context-param>  
                     HTML - generated by Spring security  
                     <body onload='document.f.j_username.focus();'>  
                     <form name='f'method='POST' action='/Spitter/j_spring_security_check'>  
                          <input type='text'name='j_username'value=''>  
                          <input type='password'name='j_password'/>  
                          <inputname="submit"type="submit"/>  
                          <inputname="reset"type="reset"/>  
                     </form>  
           - To define a specific page  
                <form-loginlogin-processing-url="/static/j_spring_security_check"  
                     login-page="/login"  
                     authentication-failure-url="/login?login_error=t"/>  
                HTML  
                <%@ taglibprefix="s"uri="http://www.springframework.org/tags"%>  
                <div>  
                     <spring:urlvar="authUrl" value="/static/j_spring_security_check"/> <-- Authentication filter path  
                     <form method="post"class="signin"action="${authUrl}">  
                          <inputid="username_or_email" name="j_username" type="text" />  
                          <inputid="password" name="j_password" type="password" />  
                          <small><a href="/account/resend_password">Forgot?</a></small>  
                          <inputid="remember_me" name="_spring_security_remember_me" type="checkbox"/>  
                          <td><inputname="commit"type="submit"value="SignIn"/></td>  
                     </form>  
                     <scripttype="text/javascript">  
                          document.getElementById('username_or_email').focus();  
                     </script>  
                </div>  
                Most important are j_username and j_password.  
           - logout  
                <logout logout-url="/static/j_spring_security_logout"/>  
           - Intercepting requests  
                First line of defense in the request-level security.  
                <intercept-urlpattern="/**"access="ROLE_USER"/> <--- All request is allowed to ROLE_USER  
                <intercept-urlpattern="/admin/**"access="ROLE_ADMIN"/> /admin/** allowed to ROLE_ADMIN  
                Rule are top to bottom. Than order of declarations is important  
           - Securing using Spring Expression (SpEL)  
                To enable:  
                <http auto-config="true"use-expressions="true">  
                     ...  
                </http>  
                <intercept-urlpattern="/admin/**"access="hasRole('ROLE_ADMIN')"/> <--- hasRole if user has granted the ROLE_ADMIN  
                Security Expressions  
                     authentication, denyAll, hasAnyRole(LIST), hasRole, hasIpAddress(IP Address), isAnonimous(), isAuthenticated() ,  
                     isFullyAuthenticated(), isRememberMe(), permitAll, principal  
                Ex:  
                     <intercept-urlpattern="/admin/**" access="hasRole('ROLE_ADMIN') and hasIpAddress('192.168.1.2')"/>  
           - Forcing request to HTTPS  
                - Sure. Sensitive information should be passed encrypted over HTTPS  
                <intercept-urlpattern="/project/form"requires-channel="https"/> <--- Redirect automatically to a https channel  
                <intercept-urlpattern="/home"requires-channel="http"/>  
           - Example  
                <http auto-config="true" use-expressions="true">  
                     <intercept-url pattern="/login" access="permitAll" />  
                     <intercept-url pattern="/logout" access="permitAll" />  
                     <intercept-url pattern="/accessdenied" access="permitAll" />  
                     <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />  
                     <form-login login-page="/login" default-target-url="/list" authentication-failure-url="/accessdenied" />  
                     <logout logout-success-url="/logout" />  
                </http>  
           - Securing view-level elements  
                Securing in view layer.  
                <%@ taglibprefix="security" uri="http://www.springframework.org/security/tags"%>  
                Elements:  
                     - <security:accesscontrollist> : allow the body of this tag to be rendered if the current authenticated user has permition  
                     - <security:authorize> : Allows the body of this tag to be rendered if a meet a specific security constraint  
                          <security:authorizeaccess="hasRole('ROLE_SPITTER')">  
                               Render something  
                          </security:authorize>  
                          or  
                          <security:authorize access="isAuthenticated() and principal.username=='habuma'">  
                               <a href="/admin">Administration</a>  
                          </security:authorize>  
                          Other parameters  
                               - access  
                               - url: The the url is allowad to some user  
                               - ifAllGranted  
                               - ifAnyGranted  
                               - ifNotGranted  
                     - <security:authentication> : Accesses properties of the current user's authentication object  
                          Hello <security:authentication property="principal.username"/>   
                          <security:authentication property="principal.username" var="loginId"/>  
                               Access the prop of principal(object).  
                          Can use: authorities(collection of GrandAuthorities), credentials(ex:password), details(additional info like IP, session ID), principal(principal's user)  
      - Authenticating Users  
           Spring supports authenticating using, but you can implements your own  
                - In-memory, JDBC-based, LDAP-based user repositories, OpenID decentralized user identity systems, X509 certificates, JASS-based providers  
           - In-memory user repository  
                configure in spring-security.xml. Hard coded  
                     <user-serviceid="userService">  
                          <user name="habuma"password="letmein" authorities="ROLE_SPITTER,ROLE_ADMIN"/>  
                          <user name="twoqubed"password="longhorns" authorities="ROLE_SPITTER"/>  
                          <user name="admin"password="admin" authorities="ROLE_ADMIN"/>  
                     </user-service>  
                     <security:authentication-manager>                                <-- Registers the authentication manager(instance of ProviderManager)  
                          <authentication-provider user-service-ref="userService"/>  
                     </security:authentication-manager>                           
           - Using DataBase(JDBC)  
                wires jdbc-user-service into <authentication-provider>'s user-service-ref, like above  
                <jdbc-user-service id="userService" data-source-ref="dataSource"/>  
                This objects uses this to get information: select username,password,enabled from users where username=?  
                to look up authorities: select username,authority from authorities where username=?  
                Whole example changing the database:  
                <jdbc-user-service id="userService"   
                     data-source-ref="dataSource"   <---- Reference to the dataSource  
                     users-by-username-query="select username,password,true from testTable where username=?" <-- to select user  
                     authorities-by-username-query="select username, authority from testTable where username=?"/> <-- Get Authorities  
                     If we dont have authorities your can replace "authority" by 'ROLE_TEST'  
      - Securing Method-level  
           Is packed in <global-method-security>  
           ex: <global-method-security secured-annotations="enabled"/>  
           Four Ways:  
                - method annotated @Secured  
                     @Secured("ROLE_USER", "ROLE_ADMIN")  
                     public void addSpittle(Spittle spittle){  
                     // ...  
                     }  
                - Method annotated with JSR-250 - @RollesAllowed  
                     Used identically as @Secured. Difference is that @RollesAllowed is standard java  
                     - <global-method-securityjsr250-annotations="enabled"/>  
                - Method annotated with Spring's pre and post invocation  
                     - Uses SpEL. More powerful than previous.  
                     - <global-method-securitypre-post-annotations="enabled"/>  
                     - Annotations  
                          @PreAuthorize Restricts access to a method before invocation based on the result of evaluating an expression  
                               @PreAuthorize("(hasRole('ROLE_USER')and#spittle.text.length()<=140) or hasRole('ROLE_PREMIUM')")  
                               public void add Spittle(Spittle spittle){  
                                    // ...  
                               }  
                          @PostAuthorize Allows a method to be invoked, but throws a security exception if the expression evaluates to false  
                               @PostAuthorize("returnObject.spitter.username==principal.username")  
                               public Spittle getSpittleById(longid){  
                                    // ...  
                               }  
                          @PreFilter Allows a method to be invoked, but filters input prior to entering the method  
                               @PreAuthorize("hasRole('ROLE_SPITTER)")  
                               @PostFilter("filterObject.spitter.username==principal.name")  
                               public List<Spittle>getABunchOfSpittles(){  
                               ...  
                               }  
                          @PostFilter Allows a method to be invoked, but filters the results of that method per an expression  
                - Method matching some explicity declared pointcuts  

Spring MVC

 - Spring MVC  

     - Flow of a requiest in Spring MVC
         - Request to DispacherServlet(the front controller)
             - HandlerMapping
             - Model and logical view name
                 - Controller (return to DispacherServlet)
             - ViewResolver
             - View
     - Setting up Spring MVC
         - Configure the web.xml
             The Servlet
             <servlet>
                 <servlet-name>spitter</servlet-name>
                 <servlet-class>
                     org.springframework.web.servlet.DispatcherServlet
                 </servlet-class>
                 <load-on-startup>1</load-on-startup>
             </servlet>
             The Mapping
             <servlet-mapping>
                 <servlet-name>spitter</servlet-name>
                 <url-pattern>/</url-pattern> Will handle all request including request from static content.
             </servlet-mapping>
         - Configure the Spring Configuration (ex: project-servelt.xml)
             <?xml version="1.0"encoding="UTF-8"?>
             <beans xmlns="http://www.springframework.org/schema/beans"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:mvc="http://www.springframework.org/schema/mvc"
                 xsi:schemaLocation="http://www.springframework.org/schema/mvc
                 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                 http://www.springframework.org/schema/beans
                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
                 <mvc:resourcesmapping="/resources/**" location="/resources/"/> Handle request for static resources
             </beans>
     - Writing Controller
         - Handler Mapping - DispatcherServlet decide which controller to dispach a request to.
             BeanNameUrlHandlerMapping
                 Maps controlers to URL that are based on the controller's BEAN name
             ControllerBeanNameHandlerMapping
                 Similar than above. Names don't requires to follow URL conventions
             ControllerClassNameHandlerMapping
                 Maps controlers to URL that are based on the controller's CLASS name as the basis of URL
             DefaultAnnotationHandlerMapping
                 Maps request to controller and controller methods that are annotated with @RequestMapping
             SimpleUrlHandlerMapping
                 Maps controllers to URL using a property collection defined in the Spring application context.
             If no handler mapping is found, uses BeanNameUrlHandlerMapping and DefaultAnnotationHandlerMapping
         - To use DefaultAnnotationHandlerMapping, include in app-servlet.xml:
             <mvc:annotation-driven/>  It includes:
                 validation support
                 message conversion
                 field formating
         Controller Example
             @Controller                <---- Declare the Controller
             public class HomeController{
                 public static final int DEFAULT_SPITTLES_PER_PAGE=25;
                 private SpitterService spitterService;
                 @Inject                        <---- Inject the bean of service
                 public HomeController(SpitterService spitterService){
                     this.spitterService=spitterService;
                 }
                 @RequestMapping({"/","/home"})    <------ Handle request for home page
                 public String showHomePage(Map<String,Object> model){ <---- Model is just a Map
                     List<Spitter> list = spitterService.getRecentSpittles(DEFAULT_SPITTLES_PER_PAGE);
                     model.put("spittles", list); <------- Data into model
                     return"home";            <---- Return view name
                 }
             }
             - To recognise the @Controller <context:component-scanbase-package="com.habuma.spitter.mvc"/> should be included in app-servlet.xml
         - Testing the controller
             public classHomeControllerTest{
             @Test
             public void shouldDisplayRecentSpittles(){
                 List<Spittle>expectedSpittles =    asList(newSpittle(),newSpittle(),newSpittle());
                 SpitterService spitterService= mock(SpitterService.class);
                 when(spitterService.getRecentSpittles(DEFAULT_SPITTLES_PER_PAGE)).thenReturn(expectedSpittles);
                 HomeControllercontroller= new HomeController(spitterService);
                 HashMap<String,Object> model=new HashMap<String,Object>();
                 String viewName=controller.showHomePage(model);
                 assertEquals("home",viewName);
                 assertSame(expectedSpittles,model.get("spittles"));
                 verify(spitterService).getRecentSpittles(DEFAULT_SPITTLES_PER_PAGE);
             }
         - Resolving Internal views
             Relating to rendering output to the user.
             Can use JSP, Velocity, FreeMarker.
             There are many View resolver implementations
                 BeanNameViewResolver, ContentNegotiatingViewResolver, FreeMarkerViewResolver, InternalResourceViewResolver, JasperReportsViewResolver, ResourceBundleViewResolver, TilesViewResolver, UrlBasedViewResolver, VelocityLayoutViewResolver, VelocityViewResolver, XmlViewResolver
             - InternalResolverViewResolver.
                 Convention-over-configuration approach
                 In app-servlet.xml
                     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                         *****
                         <propertyname="prefix"value="/WEB-INF/views/"/>
                         <propertyname="suffix"value=".jsp"/>
                     </bean>
                     Configure that al JSP will be inside /WEB-INF/views folder and will have .jsp as sufix.
                 DispatchServlet ask InternalResourceViewResolver to resolv a view.
                 InternalResourceViewResolver creates a InternalResourceView(the View) which dispatches the request to the JSP for rendering.
                 If the jsp users JSTL, we should replace InternalResourceView with JstlView by setting the viewClass property inside the block above
                     ***** <propertyname="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                     It exposes JSTL-specific request attributes so that we can use the internationalization
         - Resolving tiles view
             Templating framework for laying out of a piece of pages as fragments to make on e page.
             Register TilesViewResourver in app-servlet.xml
                 <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>
             View resolver attempts to find views that are Tiles templates.
             Also add TilesConfigurer in app-servlet.xml
             <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
                 <property name="definitions">
                     <list>
                         <value>/WEB-INF/views.xml</value>   <---- Where tiles are configured
                     </list>
                 </property>
             </bean>
             <tiles-definitions>
                 <definition name="template" template="/WEB-INF/views/main_template.jsp">
                     <put-attributename="top" value="/WEB-INF/views/tiles/spittleForm.jsp"/>
                     <put-attributename="side" value="/WEB-INF/views/tiles/signinsignup.jsp"/>
                 </definition>
                 <definition name="home" extends="template">
                     <put-attributename="content"value="/WEB-INF/views/home.jsp"/>
                 </definition>
             </tiles-definitions>
         - Defining a page
             <%@ taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
             <%@ taglibprefix="s"uri="http://www.springframework.org/tags"%>
             <%@ taglibprefix="t"uri="http://tiles.apache.org/tags-tiles"%>
             <%@ taglibprefix="fmt"uri="http://java.sun.com/jsp/jstl/fmt"%>
             <div>
                 <c:forEach var="spittle"items="${spittles}">
                     <s:urlvalue="/spitters/{spitterName}" var="spitter_url">
                         <s:param name="spitterName"    value="${spittle.spitter.username}"/>
                     </s:url>
                 </c:forEach>
             </div>
     - Handling controller input
         How to deal with a set of parameters.
         To link: http://localhost:8080/spitter/spittles?spitter=habuma
         @Controller
         @RequestMapping("/spitter")            <--- Root URL path
         public class SpitterController{
             private final SpitterService spitterService;
             @Inject
             public SpitterController(SpitterService spitterService){
                 this.spitterService=spitterService;
             }
             @RequestMapping(value="/spittles",method=GET)        <--- Handle Get Requests for /spitterSpittlers
             public String listSpittlesForSpitter(    @RequestParam("spitter") String username, Model model){ <--- The request param
                 Spitterspitter=spitterService.getSpitter(username);
                 model.addAttribute(spitter);                <---- Fill model
                 model.addAttribute(spitterService.getSpittlesForSpitter(username));
                 return "spittles/list";
             }
         }
         - Model model is just likely a Map<String,Object> with some methods: addAttribute(similar to Map.put)
     - Processing Form
         Two operations:
             Display the form
             Process the form submition
         Displaying the form
         First call the controlller
             @RequestMapping(method=RequestMethod.GET,params="new") <--- only handle this GET if pass the param new.
             public String createSpitterProfile(Model model){
                 model.addAttribute(new Spitter());  <---- Create a empty object
                 return "spitters/edit";             <---- Forward to a page
             }
         ex of call:
             http://localhost:8080/Spitter/spitters?new
         - Defining the form view
             Tiles template
                 <definitionname="spitters/edit"extends="template">
                     <put-attributename="content" value="/WEB-INF/views/spitters/edit.jsp"/>
                 </definition>
             - Write the jsp
         - Processing the Form - How to handle the form
             @RequestMapping(method=RequestMethod.POST)
             public String addSpitterFromForm(@ValidSpitter spitter, BindingResult bindingResult){
                 if(bindingResult.hasErrors()){            <--- Check validation
                     return"spitters/edit";
                 }
                 spitterService.saveSpitter(spitter);  <---- Call the service to save object
                 return "redirect:/spitters/"+spitter.getUsername(); <--- Redirect to a specific page
             }
         - Handling requests with path variable
             - http://localhost:8080/Spitter//spitters/{username}
             @RequestMapping(value="/{username}",method=RequestMethod.GET)  <------ value indicates how the link should be called
             public String showSpitterProfile(@PathVariable String username, Model model){  <---- username again indicating the path{username}
                 model.addAttribute(spitterService.getSpitter(username));
                 return "spitters/view";
             }
         - Validating Input
             Use @Valid annotation
         - Show errors in JSP
             <%@ taglibprefix="sf"uri="http://www.springframework.org/tags/form"%>
             <sf:errors path="fullName"cssClass="error"/>
         <%@ taglibprefix="sf"uri="http://www.springframework.org/tags/form"%>
             <sf:form method="POST"modelAttribute="spitter" enctype="multipart/form-data">
                 <fieldset>
                     <th><sf:label path="fullName">Fullname:</sf:label></th>
                     <td><sf:input path="fullName"size="15"/><br/>
                     <sf:errors path="fullName"cssClass="error"/>
                     <th><sf:label path="username">Username:</sf:label></th>
                     <td><sf:input path="username"size="15"maxlength="15"/>
                     <small id="username_msg">Nospaces,please.</small><br/>
                     <sf:errors path="username"cssClass="error"/>
                 </fieldset>
             </sf:form>
     Upload File
         <sf:form method="POST" modelAttribute="spitter" enctype="multipart/form-data">
         multipart/form-data makes each field be submitted as a distinct part of the POST
         <tr>
             <th><labelfor="image">Profileimage:</label></th>
             <td><inputname="image"type="file"/>
         </tr>
         Receiving the file
             @RequestMapping(method=RequestMethod.POST)
             public String addSpitterFromForm(@Valid Spitter spitter, Binding Result bindingResult,
                                             @RequestParam(value="image",required=false)    MultipartFile image){
                 if(bindingResult.hasErrors()){
                     return"spitters/edit";
                 }
                 spitterService.saveSpitter(spitter);
                 try {
                     if(!image.isEmpty()){
                         validateImage(image);
                         saveImage(spitter.getId()+".jpg",image);//
                     }
                 } catch(ImageUploadExceptione){
                     bindingResult.reject(e.getMessage());
                     return"spitters/edit";
                 }
                 return "redirect:/spitters/"+spitter.getUsername();
             }
             private void validateImage(MultipartFile image){
                 if(!image.getContentType().equals("image/jpeg")){ <--- To guarantee that the file is a jpeg
                 thrownew ImageUploadException("OnlyJPGimagesaccepted");
                 }
             }
         To upload file, spring should be configured
             <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
                 p:maxUploadSize="500000"/>
             DispatcherServlet will look for multipartResolver id. DispatcherServlet doesn't know how to deal with multipart form data.
 - Spring MVC + Jquery + AJAX
     - Maven
         jackson <--- Responsible for marshall to JSON
     - in dispatcher-servlet.xml
         <context:component-scan base-package "br.com.packageRoma"/>  <---- Defines which package to look for annotations
         <mvc:annotation-driven>                <--- Shows Spring to use annotation
     - Controller
         public class TestController {
             @RequestMapping(value="/action.htm",method=RequestMethod.POST)
             public @ResponseBody Test method(HttpServletRequest request, HttpServletResponse response) throws Exception
             {
                 Test test = new Test()
                 return test;        <---- Jackson do the marshall to JSON
             }
         }
         or
         public @ResponseBody User addUser(@ModelAttribute(value="user") User user, BindingResult result ){
             String text;
             if(!result.hasErrors()){
                 userList.add(user);
                 text = "User inserted";
             }else{    
                 text = "Couldn't insert user";
             }
             return user;
         }
     - The jsp
         <script type="text/javascript" language="javascript" src="URL/jquery"></script>
         <script type="text/javascript">
             function testCallAjax(){
                 $.ajax({
                     type: "post",
                     url: "http://localhost:8080/TestSpring/test/test.htm",
                     cache: false,                
                     data:'firstName=' + $("#firstName").val() + "&lastName=" + $("#lastName").val() + "&email=" + $("#email").val(),
                     success: function(response){
                         $('#result').html(""); <--- Clean the div that will be renderized
                         var obj = JSON.parse(response);
                         $('#result').html("First Name:- " + obj.firstName +"</br>Last Name:- " + obj.lastName);
                     },
                     error: function(){                        
                         alert('Error while request..');
                     }
                 });
             }
         </script>
         ....
         <form name="testForm" method="post">    
             <table>
                 <tr><td><input type="text" name="firstName" id="firstName" value=""></td></tr>
                 <tr><td><input type="text" name="lastName" id="lastName" value=""></td></tr>
                 <tr><td ><input type="button" value="Ajax Submit" onclick="testCallAjax();"></td></tr>
             </table>
         </form>
         <div id="result"></div> <----- div that will be renderized.
 - Spring Web Flow
     Framework that enables development of elements following a prescribed flow.
     It separes the definition of an applications's flow from the classes and views that implement the flow's behavior.
     - Install (maven)
         <dependency>
             <groupId>org.springframework.webflow</groupId>
             <artifactId>spring-webflow</artifactId>
             <version>2.3.2.RELEASE</version>
         </dependency>

sexta-feira, 30 de agosto de 2013

BDD (Behaviour-Driven Development)

- BDD (Behaviour-Driven Development)
BDD is an approach for building a shared understanding on what software to build by working through examples.
- Introdução
- It is a evolution of TDD (Test Driven Development) and of Acceptance Test Driven Planing.  Focuses in collaborating with business people.
- Relaciona o TDD com o DDD(Domain Driven Development)
- Tenta reduzir a distancia entre o Domínio e a tecnologia.  Encourage colaboration between developers and non technical areas.
- É baseado no exemplo
- Três princípios
- Negócio e tecnologia deveriam "falar" sobre o sistema da mesma forma
- Qualquer sistema deveria ter um valor identificável e verificável para o negócio
- Análise, design e planejamento prococe tem sempre retorno questionável
- Por que funciona?
- Vocabulário pequeno e bem definido.  Tenta minimizar ruídos tanto de TI quanto de negócios.
- Usa combina lingua nativa com uma linguagem Ubiqua.
- Desenvolvedores foram em "por quê" o código é criado e não em detalhes técnicos.
- Minimia o traduções entre a linguagem técnica e usuário/clientes/gerentes.
- Práticas do BDD
- Envolver partes interessadas no processo através do desenvolvimento de fora pra dentro
- Usar exemplos para descrever o comportamento de uma aplicação ou unidade de código
- Automatizar so exemplos para prover um feedback rápido e testes de regressão.
- Usar "deve" "should" para descrever comportamentos de software pra esclarecer responsabilidades
- Usar "mocks" para auxiliar na colaboração entre módulos.
- Processo
- Compete ao negócio
- Features
- Representação em alto nível as características do sistema.
- Descrição resumida dos "valores" a serem entregues(autenticação, cadastro de clientes, etc)
- Cenários
- Descrições de casos de uso.
- pré-requisitos, ações, resultado esperado
- Passos/ Etapas
- Interações entre agente externo e resultado esperado para cada cenário.
- Compete à TI
- Definição das Etapas(Step definitions): Correspondências usando ferramenta de teste.
- Implementação efetiva do código
- Simulação do usuário
- Documentação
- BDD une a documentação formal feita pelo negócio com os testes que mostram que essa documentação é válida
- Vocabulário consistente para o negócio
- Cada cenário corresponde a uma lista de etapas
- São escritos em texto simples
- Example
1 - Identify a feature : I want to sell train ticket bookings online.
How build a feature: Story -> Examples -> Automated acceptance criteria.
2 - Understand why : To increase ticket sales
3 - Find examples:
- Given Jane wants to take a specific train
- When she books a ticket online
- Then she should be charged $25
4 - Challange Assumptions
- Given Joe wants to take a specific train
- And he is has a pension card
- When she books a ticket online
- Then she should be charged $20
- And he should  be issued with a valid ticket
5 - Extend your model
- Assess all possible assumptions

TDD (Test-Driven Development) Introduction

- TDD (Test Driven Development)
- Intro
Evoloutionary approach to development which combines: test-first, fulfill the test and refactoring
Require great discipline from programmer
Enables programmer to take small solid steps when writing software
- Kinds of tests
- Unit Tests: Test internal funcionalities with small parts to see if it is working properly
- System: After the development group of users start use system fucionalities to assess the behavior
- Integration: Tests the integration between components
- Sanity: Test to determine if a new software version is performing well enough to accept it for a major test effort
- load: User hearvy loads such as web calls
- Stress: System is stress and perform under heavy load, traffic, large number of storage capacity or database.
- Performance: Verify the performance using both stress and load tests
- Acceptance: End users analises whether the application fits to the requirements
- Philosophy
- Develop and implement unit tests before writing any line of code
- First the test must fail
- Code is developed after test is developed
- Steps
- Add a test
- Run the test(must fail)
- Develop code
- Run the test


- Benefits
- Shortens the programming feedback
- Provides detailed (executable) specifications
- High-quality code
- Concrete evidence that your code works
- Prove the code works with a code
- Support evolutionary development

segunda-feira, 26 de agosto de 2013

Introdução JMS(Java Message Service) - Português

http://www.inf.ufsc.br/~frank/INE6514/JMS/     
http://www.brunobraga.com.br/tag/jms/

API para middleware MOM(Middleware Orientado a Mensagem) orientado à mensagens.  Com ela duas ou mais aplicações podem se comunicar por mensagem.

Permite a comunicação entre sistemas(aplicações ou módulos) sem a necessidade de conhecer as interfaces. diferentemente do e-mail convencional.
Só precisa ter um destinatário em comum e que compartilhem uma mesma mensagem. 
Podemos destacar o uso da JMS quando:
·         Um componente não pode depender da interface de outro componente, podendo ser facilmente substituído.
·         A aplicação deve rodar mesmo que nem todos os seus módulos estejam disponíveis.
·         Sua regra de negócio permite que você envie uma mensagem e não precise esperar uma resposta imediata.
·         EAI
JMS é dividida em 4 partes:
·         JMS Provider: Sistema que implementa as interfaces JMS e provê controle sobre algumas funções.  Ele é o elo de ligação entre os clientes.


·         Objetos administrativos: Objetos pré-configurados criados pelas ferramentas administrativas para uso dos clientes JMS.  São de dois tipos: Connection Factory e Destination.
·         Mensagens: São os objetos trocados entre os clientes(produtor-consumidor).  Podem ser de diferentes tipos.
·         Clientes JMS: São os componentes ou sistemas que produzem ou consomem as mensagens.

JMS Provider: Aplicação que fornece controle e administração aos serviços oferecidos pela JMS.  JMS Provider equivale ao Container EJB ou Servlet Container.  Ele que gerencia o envio e o recebimento das mensagens pelos clientes.  Uma de suas funções é o armazenamento de mensagens que não forem entregues.

Dois modelo de troca de mensagem:
·         Ponto a ponto ou modelo de filas  (Point-to-Point(PTP)
                Producer envia a msg para uma fila e SÓ um consumidor lê.  Producer conhece o destino.  Producer não precisa estar em execução quando o consumidor lê e vice-versa.  O Provider, de acordo com os parametros da mensagem, pode armazená-la até que sejam consumida ou seu tempo expire.




- 3 Elementos: Fila(Queue), um produtor e um conjumidor.
- Cada mensagem tem apenas um consumidor
- Produtor e consumidor não mantém relação de tempo entre eles
- Consumidor reconhece quando a mensagem é processada corretamente.
·         Publihs/subscribe(pub/sub)
                Um publicador publica a mensagem para um Topic e todos os assinantes(subscribers) registrados naquele tópico lêem a mensagem. Por default, só o Subscriber tem que estar executando para receber a mensagem, mas pode-se criar mensagens “Durable”.




A leitura das mensagens pode ser:
·         Síncrona: Chama  o método receive, onde a aplicação espera pela mensagem ou que o tempo de espera expire.
·         Assíncrona: Registra-se em um listener associado a um consumidor.  Quando a mensagem chega, o listener é avisado e ativa o processo de busca da mensagem.

Passos
1. Localizar o provedor JMS(Obtem o Driver JMS, instancia  de ConnectionFactory
2. Criar um conexão JMS 
3. Criar uma Sessão JMS
4. Localizar o destino
5. Criar um JMS Provider ou um JMS  Consumer
6. Enviar ou Receber suas mensagens


·         Soluções JMS Providers
JBossMQ, JBoss Messaging, Oracle AQ and MQSeries(www.ibm.com), SonicMQ(progress.com), OpemJMs(openjms.org)




JMS Parent
Publish-Subscribe Domain
Point-To-Point Domain
Destination
Topic
Queue
ConnectionFactory
TopicConnectionFactory
QueueConnectionFactory
Connection
TopicConnection
QueueConnection
Session
TopicSession
QueueSession
MessageProducer
TopicPublisher
QueueSender
MessageConsumer
TopicSubscriber
QueueReceiver, QueueBrowser
Table 1: Relationship of Point-To-Point and Publish-Subscribe interfaces