Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added a json response to indicate server info

  • Loading branch information...
commit 3911f1c5280241c2d8c4fbb3f916678a9c47d668 1 parent 9640155
@rodnaph authored
View
1  .gitignore
@@ -1,4 +1,5 @@
build
+dist
docs
manifest.mf
nbproject
View
2  build.xml
@@ -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" />
View
23 src/com/pugh/sockso/web/action/Jsoner.java
@@ -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;
@@ -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 );
@@ -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
*
*/
@@ -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
*
@@ -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() );
+
+ }
}
View
24 templates/com/pugh/sockso/templates/json/TServerInfo.jamon
@@ -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 %>"
+}
View
69 test/com/pugh/sockso/web/action/JsonerTest.java
@@ -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.*;
@@ -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" );
+
+
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.