001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle. 005 * 006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019 020package pt.ua.dicoogle.server.web.utils; 021 022import java.io.IOException; 023import java.util.List; 024 025import javax.servlet.http.HttpServletResponse; 026 027import net.sf.json.JSONObject; 028 029/** 030 * Common simple responses of success and failure 031 * 032 * @author Frederico Silva <fredericosilva@ua.pt> 033 */ 034public class ResponseUtil { 035 036 public static void simpleResponse(HttpServletResponse resp, String name ,boolean state) throws IOException { 037 resp.setContentType("application/json"); 038 JSONObject object = new JSONObject(); 039 object.put(name, state); 040 object.write(resp.getWriter()); 041 } 042 043 public static void objectResponse(HttpServletResponse resp, List<Pair> pairs) throws IOException { 044 resp.setContentType("application/json"); 045 JSONObject object = new JSONObject(); 046 047 for(Pair entry: pairs){ 048 object.put(entry.getKey(), entry.getValue().toString()); 049 } 050 051 object.write(resp.getWriter()); 052 } 053 054 /* 055 * Generic Pair Util for Json response 056 */ 057 public static class Pair<V>{ 058 String key; 059 V value; 060 public Pair(String key, V value) { 061 super(); 062 this.key = key; 063 this.value = value; 064 } 065 public String getKey() { 066 return key; 067 } 068 public V getValue() { 069 return value; 070 } 071 072 073 } 074}