Monday, November 16, 2009

RESTful Web Services using Eclipse and Jersey

Axis2 wraps everything in XML, and some people prefer Eclipse over Netbeans, but developing a RESTful web services project in Eclipse without Axis is possible even though it is not straight forward. Here are instructions to create a RESTful web services project in Eclipse without Axis:

Setup

  1. Download Eclipse IDE for Java EE Developers (as opposed to Java Developers) from http://www.eclipse.org/downloads/
  2. Download Glassfish from https://glassfish.dev.java.net/downloads/v2.1-b60e.html and follow the instructions on the web page.
  3. Download Jersey 1.1 from https://jersey.dev.java.net/servlets/ProjectDocumentList
  4. Install the Firefox "Poster" add-on

Create the RESTful web service project
  1. Create a new Dynamic Web Project that uses GlassFish v2.x for the Target Runtime
  2. Right-click in the Servers view and create a new GlassFish v2.x server
  3. Create a new package in the Java Resources folder
  4. Create a new Java class in the new package in the Java Resources folder (hereafter referred to as class A)
  5. In the Properties for the project, on the Java Build Path page, in the Libraries tab, click on "Add External JARs"
  6. Find the jsr311-api*.jar in the folder you installed Jersey to, and add it as an external JAR
  7. Add the @Path("insert_a_path_here") annotation right before the declaration of class A
  8. Add a method to class A that has the @GET annotation and the @Produces("application/xml") annotation right before the method declaration
  9. Create another class in the same package as class A (hereafter referred to as class B)
  10. Add the @XmlRootElement(name="insert_a_name_here") right before the declaration of class B
  11. Add some random fields to class B and generate getters and setters for them
  12. Add the @XmlElement annotation from the javax.xml.bind.annotation package right before the getters
  13. Modify class A to return a new instance of class B
  14. Replace the contents of the web.xml file in the WebContent/WEB-INF directory with
  15. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xsi="http://www.w3.org/2001/XMLSchema-instance" schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
    <servlet-name>ServletAdaptor</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>ServletAdaptor</servlet-name>
    <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>
    <session-config>
    <session-timeout>
    30
    </session-timeout>
    </session-config>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>

  16. Right-click on the project and choose Run As->Run on server.
  17. Open the Poster plug-in in Firefox and run a GET request on http://localhost:8080/insert_name_of_dynamic_web_project_here/resources/insert_the_path_specified_in_the_annotation_for_class_A_here (modify port numbers as necessary depending on output of GlassFish application deployment in the "Console" tab)
  18. You should get a XML response with the data from class B
  19. El fin!


Note: if you want to return a list of objects, use the following annotations before the getter that returns the list:

@XmlElementWrapper
@XmlElements({@XmlElement(name="element_name")})

where element_name is "suggestion" in the example XML below:

<suggestions>
<suggestion>a suggestion</suggestion>
<suggestion>another suggestion</suggestion>
</suggestions>

Also note that the web.xml and sun-web.xml contain the two parts of the URL before the part specified by the @Path annotation.

Thursday, October 22, 2009

Scalable Online Discussions

Large discussions such as the debate over national health care in the United States could be aided if people could discuss issues with each other more effectively. Generally, the pros and cons are not all gathered together into one central location - news outlets and blogs post their opinions on options they believe are best, and some even discuss both sides of the issue, but both have difficulty doing a complete treatment of the discussion. Invariably, points are left out either through ignorance or bias. But if a central discussion page were to be created that would allow all people to publish the points in the debate as they see fit, if the points were consolidated to eliminate duplication, if points were ranked by importance, and if methods were made available to allow people to collaborate about conflicts regarding the combination of points and their relative importance, the discussion could be rationally made and more people could be educated and convinced as to the rightness of the right side of the discussion.

Discussions of all kinds could take place, from the best way to fix local community problems to choosing the appropriate response to actions by multinational companies and states. Some problems need to be resolved, such as preventing a few people from stifling the opinions of others by removing points simply because they have a majority of users of the site, and to provide proper communication methods to allow people to quickly and effectively discuss resolutions to conflicts. Perhaps the site hosting the discussion pages could provide voice communication so that people can talk to each other. Methods would be put in place so that groups can choose a moderator that would take charge of giving people the floor, and methods to replace moderators with a vote in case the moderator is stifling the opinions of the opposing side of the discussion and to take the floor away from someone that is either intentionally not contributing to the discussion or is attempting to filibuster and the moderator is not already taking the floor away from said person. Such a communication system would also have to support perhaps millions of listeners and participants. The system could also allow the division into groups so that more people can voice their opinions rather than having to wait in a line of 10,000 people.

Sunday, January 18, 2009

Why I Have Not Bought an iPhone

I haven't posted in a while, so here's a post.

I haven't been keeping up with technology recently. Although I wrote some posts about the "fighting" between the hackers and Apple, I have no intention of owning an iPhone. Despite the dramatic price reduction of the 3G over the original, the cost is still to high for me to get one, and the benefit is low.

At the moment, I have a cell phone that cost about $100 that I bought specifically for its camera. With a convenient camera that is small and I should always have with me, if I'm ever in a minor accident, I can just swap information with the other driver, take some pictures proving it was not my fault, then move on. The cops don't seem to care about minor accidents, or at least the minor accident I was in a couple of years ago in a private parking lot that resulted in a dent in the van I was driving. They told me that all they could do is say that the other driver and I were present.

The phone I bought also has some nice features like a calendar, but I still use my paper planner for organizing my time and keeping track of that which I need to do. I find writing in a paper planner to be more convenient than trying to type on a tiny keyboard that is not sufficiently large to allow me to assume the hand positions necessary to type quickly.

As for being able to browse the internet, I have not suffered from not being able to check my email during the times that I am away from a computer. I figure that if someone urgently needs to contact me, they can call me. If they need to contact me, but it does not matter how soon I respond, they can send an email. I try to make sure that I check my email at least once a day (which is easy given my need to use a computer for work and school) which is a sufficient response time for most email communication.

Although the internet can help if I become lost between destinations, or if I want to find a store nearby my current location for something I need, another device does a better job in both cases: a GPS navigation system. A GPS system knows exactly where I am and what I need to do to get there. The GPS system my wife gave me for Christmas can also find places of interest near my current location, wherever that may be, in those rare circumstances that I need to find a store I was not planning on finding before I left a location with a computer.

All in all, I do not see a benefit of owning an iPhone that makes it worth the additional cost over my current cell phone ($100 to buy a phone with a decent camera, $0.18/minute which averages to about $20/month for me since I hardly use phones).

As a side note, I was thinking today how nice it would be if there was a service somewhere in the world that provided free encrypted file storage. To make the system feasible, each client would have to have special software that handled the encryption and decryption of the file system, including managing the keys, so that the user does not have to deal with them. To the user, it would be as if they were using a network file server. To the company running the servers, they would be seeing a bunch of encrypted data passing back and forth between its servers and its clients. The client software would handle the keys and not share them with the servers so that no one that has access to the servers can view the data the clients store there except the clients that own the data. The software used by the clients would have to be open source so that people could be confident that the company hosting the servers has not programmed the clients to send the keys to the company so they can view the data.

As for which encryption would be best, I'm not sure. RSA would do the job, but it isn't necessary since the same person that encrypts the data will decrypt the data. The person would have to be able to copy down the encryption key so that if the client's computer dies, they can still get setup on another computer and access their data. The benefit of such a service lies in the backing up of the data on the remote servers. People would not have to backup their data on their own, and they would have the added benefit of being able to access all of their data from any location that has access to the internet without the problems of USB drives (the limited number of writes before they die, viruses, compatability).