Send file to user using views

There are some simple steps to send a file to the user of your web application using the Play Framework:

1. After the controller is created (what I do is to copy the controller  “Application” and do a refactor rename to the name I want).

  1. Create a file named MyController (what I do is to copy the controller  “Application” and do a rename to the name I want – in this case i chose the name MyController)
  2. After this, you need to create a folder with the same name of the controller (in this case: MyController) – app/views/MyController
  3. Now in the controller, you’ve to create an index method, where you’ll output data for the user, for instance:
    1. public static void index() {
          List users = User.all().fetch();
          render(users);
      }
    2. Now you need to create a method that will create the file, or get the file somewhere:
      public static void generateMyDocument() {
             File f = new File("myFile.xls");
             //now you've to edit the file, in this is case is an excel file,
             //so you can edit using a library like JExcelAPI
             //set the header with the size of the file
             response.setHeader("Content-Length", String.valueOf(f.length()));
             //set the content type, in this case is a microsoft excel
             response.contentType = "application/ms-excel";
             //send the file to the user
             renderBinary(f);
             //after I send the file, I want to return to the same page (index())
             index();
      }
  4. Now we need to create a view, inside that folder we just created (create an index.html file):
    • <a href="@{generateMyDocument()}">Download file</a>

      We are calling the method from the controller. First we have the folder with the same name that will route directly to the method of the controller.Off course that we could do this:

    •       <a href="@{MyController.generateMyDocument()}">Download file</a>

      And that’s it. The magic on the Play Framework is done .

Posted in Internet, play, Software Development | Tagged , , , , | Leave a comment

Security tips for a web developer

Every web application has two sides. The client side and the server side. Everything on the client side can be changed, and most of the security problems happens when you trust in the client. With a simple proxy (like webscarab) you can edit the fields that were validated using javascript. That’s why you always need to validate the data on the server side. Play Framework has grown to be a great framework for this, giving advantages such as using annotations to control the type of data that came from the client side.

So, a great way to think out of box is to use those validation features inside the model, that every good MVC Framework has, and don’t trust to much on using hidden fields in the HTML that when changed can affect other users inside the web application. For instance, if you have an hidden field that has the id of the user, and if I change that, I can change others users data.

So, in conclusion:

  • You can use cookies and sessions to know which user is authenticated (inside the model you can have the stuff he can do on the web application)
  • Don’t trust in the client side. Use always server and client validation
Posted in Internet, play, Software Development | Tagged , , , , | Leave a comment

Leadership Lessons from Dancing Guy

Read transcrip at http://sivers.org/ff .

Great Lesson!

Posted in Entrepreneurship | Tagged , , | Leave a comment

Play Framework + Oracle = EASY!

To configure your oracle database using the play framework you need to follow the following steps:

  1. Choose the JAR with the drivers according to your oracle’s database version and put in the lib/ directory of your web application
  2. Configure your application.conf file,  located inside the conf/ directory
    • In the JPA Configuration Section you have to choose the JPA (Hibernate) dialect. In my case I was working with oracle10g so i had to insert in the configuration file:
      • jpa.dialect=org.hibernate.dialect.Oracle10gDialect
    • Now you just need to add the following lines, according to your database (including username and password).
      • db.url=jdbc:oracle:thin:@yourdatabaseserver:1521:dbname
        db.driver=oracle.jdbc.driver.OracleDriver
        db.user=yourusername
        db.pass=yourpassword
  3. Now you just need to restart the server. He adds the database drivers automatically to the classpath of the project.
  4. If you want, you can generate Netbeans or Eclipse projects, so you can open this projects with everything configured.

Really easy with no XMLs!

Posted in Internet, play, Software Development | Tagged , , , , | 9 Comments

[Play] Iterate over objects using jQuery

What if I want to navigate into each checkbox, radio of my html? You can do it pretty easily using jQuery.each function.

In every checkbox element I want to send a post message to my Play Framework.

jQuery.each($(':checkbox'), function() {
    $.post('@{myPlayMethod()}', { itemValue: $(this).val() })
});

This code sends the html element value property to method “myPlayMethod(String itemValue)“.

Posted in play | Tagged , , , , | Leave a comment

How to divide your GUI from your processing code?

Many students don’t know how to organize their code. Abstractions and Interfaces make a difference when developing Java applications. What if your code was divided? For instance, if you built a network application with a GUI (Graphical User Interface). The first approach is to put the code all together because it’s easier from the beginning. That’s wrong because it’s harder to find an error, and you have to read a lot of garbage code like textBox.setValue

What about dividing this network application in two layers? One layer, with all the network code and the top layer with only the GUI? How is this possible? Using Java interfaces and the notion of events.

We can start by creating our event interface that will be waken up, when for instance we are receiving a message from the network:

public interface EventNetworkInterface {
	void receiveMessage(String msg);
}

Now it’s time to build our simplified network layer:

public class NetworkLayer {
	EventNetworkInterface gui = null;

	public NetworkLayer(EventNetworkInterface gui) {
		this.gui = gui;
	}

	public void receiveMessageNetwork() {
		//simulates receiving a message from the network
                String msg_received = "Hello World!";
		//wake up event in the GUI
		gui.receiveMessage(msg_received);
	}
}

Now with the network layer created you have to create the GUI:

public class GUI implements EventNetworkInterface {

	public static void main(String[] args) {
		new GUI();
	}

	private NetworkLayer nl;

	public GUI() {
		nl = new NetworkLayer(this);
		//issue a receiveMessage event:
		nl.receiveMessageNetwork();
	}

	@Override
	public void receiveMessage(String msg) {
		System.out.println("I sent the message: " + msg);
	}

}

In the GUI constructor we are simulating a receivedMessage event after we build the network layer and send the GUI that implements the events that will be waken up when a receiveMessage (in this case) happens.

With this approach you can separate the design code, from the processing code and simplify when you are adding more features to the layers beneath you.

If you want to have multiple classes to execute the same events, see more about the observer pattern.

Posted in Software Development | Tagged , , , , | Leave a comment

Singleton Design Pattern problems?

The Singleton Pattern ensures a class has only one instance that provides a global point to access it. This design pattern is perfect for maintaining a set of configurations or other variables always available at run-time in your software.

public static Singleton getInstance() {
    if (uniqueInstance == null) {
       uniqueInstance = new Singleton();
    }
    return uniqueInstance;
}

In this code snippet, if you have a multithreaded application that executes the same class, some concurrency problems can occur. For instance if the two threads accesses to the condition at the same time, creates two different Singleton instances. How can you fix this problem? Newer virtual machines (like JVM 1.5) ensures this when you create the Singleton instance in a static variable outside of any method. The JVM guarantees that the instance will be created before any thread accesses the static uniqueInstance variable. The solution for this is:

public class Singleton {
    private static Singleton uniqueInstance = new Singleton ();

    private Singleton() {}

    public static Singleton getInstance () {
       return uniqueInstance;
    }
}
Posted in Software Development | Tagged , , , , | Leave a comment

Entrepreneur, remember what you thought when you were a kid?

Posted in Entrepreneurship | Tagged , , , | Leave a comment

80/20 rule for entrepreneurs

What is the 80/20 rule and why is it so important to entrepreneurs? Wikipedia says:

“The principle was suggested by management thinker Joseph M. Juran. It was named after the Italian economist Vilfredo Pareto, who observed that 80% of income in Italy was received by 20% of the Italian population. The assumption is that most of the results in any situation are determined by a small number of causes.”

80-20-ruleThis rule says that 80% of your outcomes come from 20% of your inputs. For instance, in your life, there are certain activities that you do (your 20%) that account for the majority (your 80%) of your happiness and outputs.

The 80/20 rule can be applied to your life and your business, where being happy and satisfied for what you do, is the most important thing. Off course that money is important, but it shouldn’t be so important compared to your happiness.

Live your life following the 80/20 rule. Follow your dream!

(more information about the 80/20 rule)

Posted in Entrepreneurship | Tagged , | Leave a comment