package votorola.s.wap; // Copyright 2012, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.io.*; import javax.servlet.http.*; import votorola.a.count.*; import votorola.a.web.wap.*; import votorola.g.io.*; import votorola.g.lang.*; import votorola.g.web.*; /** A web API for listing compiled polls, * which are defined as those with counts reported through * vocount. Calls to this interface are conventionally prefixed by 'ps' * (wCall=psPollspace). If you choose a different prefix, then adjust the * parameter names below accordingly. An example request is: * *
http://reluk.ca:8080/v/wap?wCall=psPollspace&wPretty
* *

The response includes the following components. These are shown in JSON format * with explanatory comments:

 {
  *    "ps": [ // or other prefix, per {@linkplain WAP wCall} query parameter
  *       // Compiled polls in {@linkplain votorola.a.count.PollService#compareTo(PollService) natural order}:
  *       {
  *          "{@linkplain Poll#name() name}": "POLL NAME 1",
  *          "{@linkplain Poll#displayTitle() displayTitle}": "DISPLAY TITLE", // if any
  *          "{@linkplain Poll#issueType() issueType}": "ISSUE TYPE"
  *       },
  *       {
  *          "{@linkplain Poll#name() name}": "POLL NAME 2",
  *          "{@linkplain Poll#issueType() issueType}": "ISSUE TYPE"
  *       }
  *       // and so on, up to {@value MAX_RESPONSE_SIZE}
  *    ]
  * }
* * @see vocount */ public @ThreadRestricted("constructor") final class PollspaceWAP extends Call { /** Constructs a PollspaceWAP. * * @see #prefix() * @see #req() */ public PollspaceWAP( final String prefix, final Requesting req, final ResponseConfiguration resConfig ) throws HTTPRequestException { super( prefix, req, resConfig ); final File readyToReportLink = req.wap().vsRun().voteServer().scopeCount().readyToReportLink(); if( readyToReportLink.exists() ) { try { final ReadyDirectory dir = new ReadyDirectory( readyToReportLink.getCanonicalPath() ); pollspace0CacheOrNull = req.wPretty()? dir.pollspace0PCache(): dir.pollspace0Cache(); } catch( IOException x ) { throw new RuntimeException( x ); } } else pollspace0CacheOrNull = null; } // ------------------------------------------------------------------------------------ /** The name to use in the {@link WAP wCall} query parameter, which is {@value}. For * example: wCall=psPollspace. */ public static final String CALL_TYPE = "Pollspace"; /** The maximum number of polls returned per response. No paging mechanism is * provided yet, so polls beyond this number are currently inaccessible. */ public static final int MAX_RESPONSE_SIZE = 500; // - C a l l -------------------------------------------------------------------------- public void respond( final Responding res ) throws java.io.IOException { res.outJSON().flush(); final Writer outBuf = res.outBuf(); final boolean wPretty = res.wPretty(); if( wPretty ) outBuf.append( '\n' ).append( res.outJSONIndent() ); outBuf.append( '"' ).append( prefix() ).append( "\":" ); if( wPretty ) outBuf.append( ' ' ); if( pollspace0CacheOrNull == null ) outBuf.append( "[]" ); else { FileX.appendTo( outBuf, pollspace0CacheOrNull, java.nio.charset.StandardCharsets.UTF_8 ); } // if( wPretty ) outBuf.append( '\n' ); /// but prettier without that, as indentation slightly off in pollspace0CacheOrNull } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final File pollspace0CacheOrNull; }