Java tutorial
/* * Copyright 2015 Robert von Burg <eitch@eitchnet.ch> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ch.eitchnet.csvrestendpoint.resources; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.BeanParam; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import ch.eitchnet.csvrestendpoint.components.CsvDataHandler; import ch.eitchnet.csvrestendpoint.marshaller.CsvDataMarshaller; import ch.eitchnet.csvrestendpoint.marshaller.CsvDataToHeaderJsonMarshaller; import ch.eitchnet.csvrestendpoint.marshaller.CsvDataToJsonMarshaller; import ch.eitchnet.csvrestendpoint.marshaller.CsvNamesToJsonMarshaller; import ch.eitchnet.csvrestendpoint.model.CsvQuery; import ch.eitchnet.csvrestendpoint.util.Result; import ch.eitchnet.utils.helper.ExceptionHelper; import li.strolch.rest.RestfulStrolchComponent; /** * @author Robert von Burg <eitch@eitchnet.ch> */ @Path("csv") public class CsvResource { private static final Logger logger = LoggerFactory.getLogger(CsvResource.class); @GET @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Response getNames() { logger.info("Received request for names."); try { CsvDataHandler csvDataHandler = RestfulStrolchComponent.getInstance() .getComponent(CsvDataHandler.class); List<String> names = csvDataHandler.getCsvNames(); CsvNamesToJsonMarshaller marshaller = new CsvNamesToJsonMarshaller(); JsonObject root = marshaller.marshall(names); Gson gson = new GsonBuilder().setPrettyPrinting().create(); String entity = gson.toJson(root); return Response.ok(entity, MediaType.APPLICATION_JSON).build(); } catch (Exception e) { logger.error(e.getMessage(), e); Result result = new Result(ExceptionHelper.getExceptionMessage(e)); String entity = new Gson().toJson(result); return Response.ok(entity, MediaType.APPLICATION_JSON).build(); } } @GET @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @Path("{name}/headers") public Response getHeaderFor(@PathParam("name") String name, @BeanParam CsvQuery query) { logger.info("Received request for: " + name); try { CsvDataToHeaderJsonMarshaller marshaller = new CsvDataToHeaderJsonMarshaller(query); return queryData(name, marshaller); } catch (Exception e) { logger.error(e.getMessage(), e); Result result = new Result(ExceptionHelper.getExceptionMessage(e)); String entity = new Gson().toJson(result); return Response.ok(entity, MediaType.APPLICATION_JSON).build(); } } @GET @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @Path("{name}") public Response getDataFor(@PathParam("name") String name, @BeanParam CsvQuery query, @Context HttpServletRequest request) { logger.info("Received request for: " + name); try { CsvDataToJsonMarshaller marshaller = new CsvDataToJsonMarshaller(query); return queryData(name, marshaller); } catch (Exception e) { logger.error(e.getMessage(), e); Result result = new Result(ExceptionHelper.getExceptionMessage(e)); String entity = new Gson().toJson(result); return Response.ok(entity, MediaType.APPLICATION_JSON).build(); } } private Response queryData(String name, CsvDataMarshaller<JsonObject> marshaller) { CsvDataHandler csvDataHandler = RestfulStrolchComponent.getInstance().getComponent(CsvDataHandler.class); JsonObject root = csvDataHandler.getCsvData(name, marshaller); if (root == null) throw new RuntimeException("No CSV Data exists with name '" + name + "'"); Gson gson = new GsonBuilder().setPrettyPrinting().create(); String entity = gson.toJson(root); return Response.ok(entity, MediaType.APPLICATION_JSON_TYPE.withCharset(marshaller.getCharset().name())) .build(); } }