Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
jreyes committed Feb 23, 2012
1 parent abdf52e commit 3621442
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
@@ -0,0 +1,8 @@
*.iml
*.class
.idea

# Package Files #
*.jar
*.war
*.ear
52 changes: 52 additions & 0 deletions pom.xml
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.vaporwarecorp.rest</groupId>
<artifactId>rest-client</artifactId>
<version>1.0</version>
<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.6</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
30 changes: 30 additions & 0 deletions src/main/java/com/vaporwarecorp/rest/client/DateAdapter.java
@@ -0,0 +1,30 @@
package com.vaporwarecorp.rest.client;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateAdapter
extends XmlAdapter<String, Date>
{
// ------------------------------ FIELDS ------------------------------

private SimpleDateFormat dateFormat = new SimpleDateFormat( "EEE, dd MMM yyyy HH:mm:ss ZZZZZ", Locale.ENGLISH );

// -------------------------- OTHER METHODS --------------------------

@Override
public String marshal( Date date )
throws Exception
{
return dateFormat.format( date );
}

@Override
public Date unmarshal( String date )
throws Exception
{
return dateFormat.parse( date );
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/vaporwarecorp/rest/client/RestClient.java
@@ -0,0 +1,46 @@
package com.vaporwarecorp.rest.client;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.util.Scanner;

public final class RestClient
{
// --------------------------- main() method ---------------------------

public static void main( String[] args )
{
Scanner input = new Scanner( System.in );
System.out.println( "Buscar en Twitter: " );
String busqueda = input.nextLine();

SimpleDateFormat dateFormat = new SimpleDateFormat( "MM/dd/yy HH:mm" );

// crea el cliente
ClientConfig config = new DefaultClientConfig();
Client client = Client.create( config );

// donde se encuentra el servidor REST que queremos usar
WebResource resource = client.resource( "http://search.twitter.com/search.json" );

// ahora hacer la busqueda en twitter
SearchResult result =
resource.queryParam( "q", busqueda ).queryParam( "rpp", "5" ).accept( MediaType.APPLICATION_JSON ).get(
SearchResult.class );

// cojamos la lista de tweets y enseñemos el tweet en la consola
for ( Tweet tweet : result.getTweets() )
{
String s = String.format( "[%s][%s] %s",
dateFormat.format( tweet.getFechaDeCreacion() ),
tweet.getNombreDelUsuario(),
tweet.getTexto() );
System.out.println( s );
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/vaporwarecorp/rest/client/SearchResult.java
@@ -0,0 +1,33 @@
package com.vaporwarecorp.rest.client;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

@XmlRootElement
public class SearchResult
{
// ------------------------------ FIELDS ------------------------------

private List<Tweet> tweets;

// --------------------------- CONSTRUCTORS ---------------------------

public SearchResult()
{

}

// --------------------- GETTER / SETTER METHODS ---------------------

@XmlElement(name = "results")
public List<Tweet> getTweets()
{
return tweets;
}

public void setTweets( List<Tweet> tweets )
{
this.tweets = tweets;
}
}
61 changes: 61 additions & 0 deletions src/main/java/com/vaporwarecorp/rest/client/Tweet.java
@@ -0,0 +1,61 @@
package com.vaporwarecorp.rest.client;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.Date;

@XmlRootElement
public class Tweet
{
// ------------------------------ FIELDS ------------------------------

private Date fechaDeCreacion;

private String nombreDelUsuario;

private String texto;

// --------------------------- CONSTRUCTORS ---------------------------

public Tweet()
{

}

// --------------------- GETTER / SETTER METHODS ---------------------

@XmlElement(name = "created_at")
@XmlJavaTypeAdapter(DateAdapter.class)
public Date getFechaDeCreacion()
{
return fechaDeCreacion;
}

public void setFechaDeCreacion( Date fechaDeCreacion )
{
this.fechaDeCreacion = fechaDeCreacion;
}

@XmlElement(name = "from_user_name")
public String getNombreDelUsuario()
{
return nombreDelUsuario;
}

public void setNombreDelUsuario( String nombreDelUsuario )
{
this.nombreDelUsuario = nombreDelUsuario;
}

@XmlElement(name = "text")
public String getTexto()
{
return texto;
}

public void setTexto( String texto )
{
this.texto = texto;
}
}

0 comments on commit 3621442

Please sign in to comment.