Skip to content

Commit

Permalink
added a json response to indicate server info
Browse files Browse the repository at this point in the history
  • Loading branch information
rodnaph committed Mar 17, 2011
1 parent 9640155 commit 3911f1c
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build
dist
docs
manifest.mf
nbproject
Expand Down
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public class Sockso {
<!-- builds resources -->
<target name="resources" depends="init">
<!-- compile stub file -->
<javac srcdir="${dir.resources}" destdir="${dir.resources}" />
<javac srcdir="${dir.resources}" destdir="${dir.resources}" includeantruntime="true" />
<!-- remove locale javascript files, we don't want to pack them -->
<delete>
<fileset dir="${dir.resources}/htdocs/js" includes="locale*.js" />
Expand Down
23 changes: 16 additions & 7 deletions src/com/pugh/sockso/web/action/Jsoner.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.pugh.sockso.templates.json.TTracks;
import com.pugh.sockso.templates.json.TTracksForPath;
import com.pugh.sockso.templates.json.TSimilarArtists;
import com.pugh.sockso.templates.json.TServerInfo;

import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down Expand Up @@ -90,6 +91,8 @@ else if ( type.equals("similarArtists") )
similarArtists();
else if ( type.equals("tracks") )
tracks();
else if ( type.equals("serverinfo") )
serverinfo();

else throw new BadRequestException( "Unknown json request (" + type + ")", 400 );

Expand Down Expand Up @@ -545,10 +548,6 @@ public int compare( final File file1, final File file2 ) {
* a single integer which is the playlist ID if all goes well, otherwise
* you'll get a description of the problem.
*
* @param req the request object
* @param res the response object
* @param user the current user
*
* @throws IOException
*
*/
Expand Down Expand Up @@ -595,9 +594,6 @@ else if ( user == null )
* performs a search on the music collection for the specified string and
* then creates a json results page
*
* @param res the response object
* @param query the query string
*
* @throws SQLException
* @throws IOException
*
Expand All @@ -615,5 +611,18 @@ public void search() throws SQLException, IOException {
getResponse().showJson( tpl.makeRenderer() );

}

/**
* Returns information about this server (nothing secret)
*
*/
protected void serverinfo() throws IOException {

final TServerInfo tpl = new TServerInfo();
tpl.setProperties( getProperties() );

getResponse().showJson( tpl.makeRenderer() );

}

}
24 changes: 24 additions & 0 deletions templates/com/pugh/sockso/templates/json/TServerInfo.jamon
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

<%import>
com.pugh.sockso.Constants;
com.pugh.sockso.Sockso;
com.pugh.sockso.Properties;
com.pugh.sockso.Utils;
</%import>

<%args>
Properties properties = null;
</%args>

<%java>
String requiresLogin = properties.get( Constants.WWW_USERS_REQUIRE_LOGIN ) == Properties.YES
? "1"
: "0";
</%java>

{
"title": "<% properties.get(Constants.WWW_TITLE).replace("\"","\\\"") %>",
"tagline": "<% properties.get(Constants.WWW_TAGLINE).replace("\"","\\\"") %>",
"version": "<% Sockso.VERSION %>",
"requiresLogin": "<% requiresLogin %>"
}
69 changes: 69 additions & 0 deletions test/com/pugh/sockso/web/action/JsonerTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@

package com.pugh.sockso.web.action;

import com.pugh.sockso.Constants;
import com.pugh.sockso.Properties;
import com.pugh.sockso.StringProperties;
import com.pugh.sockso.tests.TestResponse;
import com.pugh.sockso.tests.SocksoTestCase;
import com.pugh.sockso.music.Artist;
import com.pugh.sockso.music.Track;
import com.pugh.sockso.tests.TestRequest;
import com.pugh.sockso.tests.TestUtils;
import com.pugh.sockso.web.*;

Expand Down Expand Up @@ -80,4 +84,69 @@ public void testGetOrderedFiles() throws Exception {

}

public void testReturningJsonServerInfoIncludesAllRequiredFields() throws Exception {

Properties p = new StringProperties();
p.set( Constants.WWW_TITLE, "THEtitle" );
p.set( Constants.WWW_TAGLINE, "THEtagline" );

TestResponse res = new TestResponse();
Jsoner j = new Jsoner( null );
j.setProperties( p );
j.setResponse( res );
j.serverinfo();

String data = res.getOutput();

assertContains( data, "title" );
assertContains( data, "THEtitle" );

assertContains( data, "tagline" );
assertContains( data, "THEtagline" );

assertContains( data, "version" );
//assertContains( data, Sockso.VERSION ); // ?

assertContains( data, "requiresLogin" );
assertContains( data, "0" );

}

public void testServerInfoReturnsRequireLoginAsOneWhenItIsEnabled() throws Exception {

Properties p = new StringProperties();
p.set( Constants.WWW_USERS_REQUIRE_LOGIN, Properties.YES );

TestResponse res = new TestResponse();
Jsoner j = new Jsoner( null );
j.setResponse( res );
j.setProperties( p );
j.serverinfo();

String data = res.getOutput();

assertContains( data, "requiresLogin\": \"1\"" );

}

public void testDoubleQuotesAreEscapedInServerInfoStrings() throws Exception {

Properties p = new StringProperties();
p.set( Constants.WWW_TITLE, "THE\"title" );
p.set( Constants.WWW_TAGLINE, "THE\"tagline" );

TestResponse res = new TestResponse();
Jsoner j = new Jsoner( null );
j.setProperties( p );
j.setResponse( res );
j.serverinfo();

String data = res.getOutput();

assertContains( data, "THE\\\"title" );
assertContains( data, "THE\\\"tagline" );


}

}

0 comments on commit 3911f1c

Please sign in to comment.