Java tutorial
/** * * FENIX (Food security and Early warning Network and Information Exchange) * * Copyright (c) 2011, by FAO of UN under the EC-FAO Food Security Information for Action Programme * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package org.fao.fenix.wds.web.rest.faostat; import com.google.gson.Gson; import org.fao.fenix.wds.core.bean.DatasourceBean; import org.fao.fenix.wds.core.bean.SQLBean; import org.fao.fenix.wds.core.datasource.DatasourcePool; import org.fao.fenix.wds.core.exception.WDSExceptionStreamWriter; import org.fao.fenix.wds.core.jdbc.JDBCIterable; import org.fao.fenix.wds.core.sql.Bean2SQL; import org.fao.fenix.wds.core.sql.faostat.SQLBeansRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.StreamingOutput; import java.io.*; import java.sql.SQLException; /** * @author <a href="mailto:guido.barbaglia@fao.org">Guido Barbaglia</a> * @author <a href="mailto:guido.barbaglia@gmail.com">Guido Barbaglia</a> * */ @Component @Path("/domains") public class FAOSTATDomainsRESTService { @Autowired DatasourcePool datasourcePool; @GET @Produces(MediaType.APPLICATION_JSON) @Path("{datasource}/{groupCode}/{language}") public Response getDomains(@PathParam("datasource") final String datasource, @PathParam("groupCode") final String groupCode, @PathParam("language") final String language) { // Initiate the stream StreamingOutput stream = new StreamingOutput() { @Override public void write(OutputStream os) throws IOException, WebApplicationException { // Initiate utilities Writer writer = new BufferedWriter(new OutputStreamWriter(os)); Gson g = new Gson(); // compute result DatasourceBean db = datasourcePool.getDatasource(datasource.toUpperCase()); SQLBean sql = SQLBeansRepository.getDomains(language, groupCode); JDBCIterable it = new JDBCIterable(); try { // Query DB it.query(db, Bean2SQL.convert(sql).toString()); } catch (IllegalAccessException e) { WDSExceptionStreamWriter.streamException(os, ("Method 'getDomains' thrown an error: " + e.getMessage())); } catch (InstantiationException e) { WDSExceptionStreamWriter.streamException(os, ("Method 'getDomains' thrown an error: " + e.getMessage())); } catch (SQLException e) { WDSExceptionStreamWriter.streamException(os, ("Method 'getDomains' thrown an error: " + e.getMessage())); } catch (ClassNotFoundException e) { WDSExceptionStreamWriter.streamException(os, ("Method 'getDomains' thrown an error: " + e.getMessage())); } catch (Exception e) { WDSExceptionStreamWriter.streamException(os, ("Method 'getDomains' thrown an error: " + e.getMessage())); } // write the result of the query writer.write("["); while (it.hasNext()) { writer.write(g.toJson(it.next())); if (it.hasNext()) writer.write(","); } writer.write("]"); // Convert and write the output on the stream writer.flush(); } }; /* Stream result */ return Response.status(200).entity(stream).build(); } }