ListDataServicesServlet extends BaseCartePlugin { @Override public void handleRequest( CarteRequest request ) throws IOException { final List<IThinServiceInformation> serviceInformation; try { Map<String, Collection<String>> requestParameters = request.getParameters(); if ( requestParameters.isEmpty() || !requestParameters.containsKey( "serviceName" ) ) { serviceInformation = client.getServiceInformation(); } else { serviceInformation = Lists.newArrayList(); if ( requestParameters.containsKey( "serviceName" ) ) { Collection<String> servicesNames = requestParameters.get( "serviceName" ); for ( String serviceName : servicesNames ) { ThinServiceInformation service = client.getServiceInformation( URLDecoder.decode( serviceName, CONTENT_CHARSET ) ); if ( service != null ) { serviceInformation.add( service ); } } } } } catch ( SQLException e ) { String msg = "Failed to retrieve service info"; logError( msg, e ); request.respond( 500 ).withMessage( msg ); return; } request.respond( 200 ) .with( "text/xml; charset=utf-8", new WriterResponse() { private void writeServiceXml( PrintWriter writer, IThinServiceInformation thinServiceInformation ) throws IOException { writer.println( XMLHandler.openTag( XML_TAG_SERVICE ) ); writer.println( XMLHandler.addTagValue( "name", thinServiceInformation.getName() ) ); writer.println( XMLHandler.addTagValue( "streaming", thinServiceInformation.isStreaming() ) ); writer.println( thinServiceInformation.getServiceFields().getMetaXML() ); writer.println( XMLHandler.closeTag( XML_TAG_SERVICE ) ); } @Override public void write( PrintWriter writer ) throws IOException { writer.println( XMLHandler.getXMLHeader() ); writer.println( XMLHandler.openTag( XML_TAG_SERVICES ) ); for ( IThinServiceInformation thinServiceInformation : serviceInformation ) { String streamingParam = request.getParameter( "streaming" ); if ( streamingParam == null ) { writeServiceXml( writer, thinServiceInformation ); } else { boolean streaming = Boolean.parseBoolean( request.getParameter( "streaming" ) ); if ( streaming == thinServiceInformation.isStreaming() ) { writeServiceXml( writer, thinServiceInformation ); } } } writer.println( XMLHandler.closeTag( XML_TAG_SERVICES ) ); } } ); } ListDataServicesServlet( DataServiceClient client ); @Override void handleRequest( CarteRequest request ); String getContextPath(); static final String CONTEXT_PATH; static final String XML_TAG_SERVICES; static final String XML_TAG_SERVICE; }
@Test public void testFailure() throws SQLException, IOException { when( client.getServiceInformation() ).thenThrow( sqlException ); when( carteRequest.respond( 500 ) ).thenReturn( carteResponse ); servlet.handleRequest( carteRequest ); verify( log ).logError( any( String.class ), any( SQLException.class ) ); verify( carteResponse ).withMessage( any( String.class ) ); }
@Test public void testRequestGetServiceNameParameter() throws Exception { ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.builder(); builder.put( "serviceName", mockServiceName ); ThinServiceInformation thinServiceInformation = new ThinServiceInformation( DATA_SERVICE_NAME, false, rowMetaInterface ); when( carteRequest.getParameters() ).thenReturn( builder.build().asMap() ); when( client.getServiceInformation( mockServiceName ) ).thenReturn( thinServiceInformation ); when( carteRequest.respond( 200 ) ).thenReturn( carteResponse ); servlet.handleRequest( carteRequest ); verify( client, times( 1 ) ).getServiceInformation( mockServiceName ); verify( client, times( 0 ) ).getServiceInformation(); }
@Test public void testRequestGetServiceNameAndStreamingParameter() throws Exception { ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.builder(); builder.put( "serviceName", mockServiceName ); builder.put( "streaming", "true" ); ThinServiceInformation thinServiceInformation = new ThinServiceInformation( DATA_SERVICE_NAME, false, rowMetaInterface ); when( carteRequest.getParameters() ).thenReturn( builder.build().asMap() ); when( client.getServiceInformation( mockServiceName ) ).thenReturn( thinServiceInformation ); when( carteRequest.respond( 200 ) ).thenReturn( carteResponse ); servlet.handleRequest( carteRequest ); verify( client, times( 1 ) ).getServiceInformation( mockServiceName ); verify( client, times( 0 ) ).getServiceInformation(); }
@Test public void testRequestGetStreamingParameter() throws Exception { ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.builder(); builder.put( "streaming", "true" ); ThinServiceInformation thinServiceInformation = new ThinServiceInformation( DATA_SERVICE_NAME, true, rowMetaInterface ); List<IThinServiceInformation> thinServiceInformationList = new ArrayList<>(); thinServiceInformationList.add( thinServiceInformation ); when( carteRequest.getParameters() ).thenReturn( builder.build().asMap() ); when( client.getServiceInformation() ).thenReturn( thinServiceInformationList ); when( carteRequest.respond( 200 ) ).thenReturn( carteResponse ); servlet.handleRequest( carteRequest ); verify( client, times( 0 ) ).getServiceInformation( anyString() ); verify( client, times( 1 ) ).getServiceInformation(); }