getContact.java

package com.workbenchclassic;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.json.JSONException;
import org.json.JSONObject;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

@Path("/getContact")
public class getContact {
    private static final DBService DB_SERVICE = new DBService("dsMesMiiNJTA");
    private static final ObjectMapper MAPPER = new ObjectMapper();

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response getUserResponse(String jsonBody) {
        String station;
        try {
            JSONObject json = new JSONObject(jsonBody);
            station = json.getString("station");
        } catch (JSONException e) {
            return Response.status(Response.Status.BAD_REQUEST)
                    .entity("{\"error\":\"Missing or invalid 'station' parameter\"}")
                    .build();
        }

        String query = "select  gad.artikel, gad.artbez, chst.cust_herst_nr as contact_code , chst.name as contact_name, gah.artikel as part_ext, gah.artbez as art_bez_ext, chst.ishersteller as IS_MANUFACTURER, chst.iskunde as IS_CUSTOMER, chst.islieferant as IS_SUPPLIER "
                +
                "from glo.adis gad join glo.adis_herst gah on  gah.object_id = gad.object_id join ml.cust_herst chst  on chst.cust_herst_id = gah.herst_id "
                +
                "where werk_id in (SELECT werk_id FROM bde.kast WHERE kap_nr = '" + station + "')";

        List<Map<String, Object>> rows;
        try {
            rows = DB_SERVICE.dbConnectAndGetRows(query);
            return Response.ok(MAPPER.writeValueAsString(rows)).build();
        } catch (SQLException e) {
            String errJson = String.format("{\"error\":\"DB error: %s\"}", e.getMessage());
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                    .entity(errJson)
                    .build();
        } catch (JsonProcessingException e) {
            String errJson = String.format("{\"error\":\"JSON processing error: %s\"}", e.getMessage());
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                    .entity(errJson)
                    .build();
        }
    }
}