Wednesday, March 5, 2014

Blank template JavaScript application for JSON requests

Blank template application for JSON requests with and without body:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>My Blank JS / jQuery app - jsFiddle demo by jorooo</title>
 
  <script type='text/javascript' src='http://code.jquery.com/jquery-latest.js'></script>
  <!--link rel="stylesheet" type="text/css" href="/css/result-light.css"-->
  <script type='text/javascript' src="http://code.jquery.com/mobile/latest/jquery.mobile.min.js"></script>
  <link rel="stylesheet" type="text/css" href="http://code.jquery.com/mobile/latest/jquery.mobile.min.css">
     
  <style type='text/css'>
   
  </style>
</head>
<body>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
    $("#alert").bind("click", function(event) {
        alert('alert called');
    }); 

    $("#call_json_with_body").bind("click", function(event) {
        $.ajax({
            success: function( data ){
                alert("success");
                alert('success, data received is ' + JSON.stringify(data));
            },
            error: function( jqXHR, textStatus, errorThrown ){
                alert( "error  while making ajax call. Error parameters are, " +
                    "\ntextStatus: " + textStatus +
                    ";\n errorThrown: " + errorThrown +
                    ";\n jqXHR: " + JSON.stringify(jqXHR) );
            },
            beforeSend: function(req) {
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type","application/json");
            },       
            dataType: 'json',
            data: JSON.stringify( {"zzz":"222"} ),
            type: 'POST',
            cache: false,
            url: "http://localhost:8080/add_venue_raw",
          }
        );   
    }); 
    $("#call_json_without_body").bind("click", function(event) {
        $.ajax({
            success: function( data ){
                alert("x");
                alert('success, data received is ' + JSON.stringify(data) );
            },
            error: function( jqXHR, textStatus, errorThrown ){
                alert( "error  while making ajax call. Error parameters are, " +
                    "\ntextStatus: " + textStatus +
                    ";\n errorThrown: " + errorThrown +
                    ";\n jqXHR: " + JSON.stringify(jqXHR) );
            },
            type: 'GET',
            cache: false,
            url: "http://localhost:8080/user/1",
          }
        );   
    }); 
});//]]> 

</script>

  <div data-role="page" id="p1">
    <div  data-role="header"><h1>Header</h1></div>
   
    <div  data-role="content">
        <h5>If the script is running from a file:/// URL you could experience some issues. It's best not to do AJAX requests from file URLs, because they are treated inconsistently. Chrome, for example, entirely disallows them. Firefox also.</h5>
        <a href="#" id="call_json_without_body" data-role="button">JSON request without body</a><br/>
        <a href="#" id="call_json_with_body" data-role="button">JSON request with body</a><br/>
        <a href="#" id="alert" data-role="button">Show alert</a>
    </div>
   
    <div  data-role="footer"><h4>Footer</h4></div>
</div>

 
</body>
</html>





static root folder for maven projects

Static files could be copied here:
<maven-project-root>\src\main\webapp\resources

They are accessed on server on URL:
<server_address>/resources

Example:
<maven-project-root>\src\main\webapp\resources\x.txt
could be accessed on
http://localhost:8080/resources/x.txt

In come cases, the content changes are actualized without server restart. For example with:
mvn jetty:run

Tuesday, February 25, 2014

Avoiding CORS security issues

CORS is security policy, according which a page could not be iframe-d from page from another site.

ajax requests are implemented via iframe, so if we want to do ajax requests to another site, we encounter CORS error.

How to enable this?

Solution is to tell server to allow the page/JSON/webservice to be iframed. This is done this way:
    public String getUserById(@PathVariable("id") Long id, HttpServletResponse httpResponse) throws JsonProcessingException {
        httpResponse.addHeader("Access-Control-Allow-Origin", "*");
        // do real work

    }

Access-Control-Allow-Origin is the header that allows this. Check wikipedia for other solutions.

solved: Spring + Hibernate used with JPA and EntityManager => HibernateException: collection is not associated with any session

When I started using lazy loading in Spring + Hibernate project, I encounter this error.

What does NOT solve my issue:
  • @Transactional annotation
  • OpenSessionInViewInterceptor


The error message was:

HTTP ERROR 500

Problem accessing /user/1. Reason:
    Server Error

Caused by:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: collection is not associated with any session
 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)


Solution for Spring with JPA approach and EntityManager, is to use OpenEntityManagerInViewFilter. Register it in web.xml this way:

Thursday, August 15, 2013

Jenkins + Multiple SCMs + git + hooks/triggers = mission Possible

Multiple SCMs plugin version 0.2 - in its documentation is remarked two controversial statements:
  • "Post-commit type triggers don't currently work".
  • "Allow polling to work with multiple instances" - in version 0.2
So is pooling possible? There are people on the internet stating the two opposite points. Yes, it is possible but some tricks are needed.

Friday, August 9, 2013

linux: demonizing a process and killing it when you want

This is very useful for processes which do not write an PID file. This is stupid... but it could be work-arounded.

For example I want to start ant kill later the jasmine server, which is started this way:
bundle exec rake jasmine:server --trace