BrightSide Workbench Full Report + Source Code
ElephantResponse.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.http;
20 
21 import com.google.gson.Gson;
22 import com.google.gson.GsonBuilder;
23 import com.google.gson.JsonSyntaxException;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.io.Reader;
27 import java.nio.charset.Charset;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import javax.servlet.http.HttpServletResponse;
31 import org.apache.commons.io.Charsets;
32 import org.apache.hc.core5.http.ContentType;
33 import org.apache.hc.core5.http.HttpEntity;
34 import org.turro.elephant.context.ElephantContext;
35 
40 public class ElephantResponse {
41 
42  public String type;
43  public String message;
44  public String code;
45 
46  public static boolean isCorrect(ElephantResponse er) {
48  }
49 
51  try {
52  return er != null ? ElephantResponseType.valueOf(er.type) : null;
53  } catch(IllegalArgumentException |NullPointerException ex) {
54  return null;
55  }
56  }
57 
58  public static ElephantResponse getResponse(HttpEntity entity) throws IOException {
59  Gson gson = new GsonBuilder().create();
60  ContentType contentType = ContentType.create(entity.getContentType(), Charsets.toCharset(entity.getContentEncoding()));
61  Charset charset = contentType.getCharset();
62  Reader reader = new InputStreamReader(entity.getContent(), charset);
63  try {
64  return gson.fromJson(reader, ElephantResponse.class);
65  } catch(JsonSyntaxException ex) {
66  Logger.getLogger(ElephantResponse.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
67  }
68  return null;
69  }
70 
71  public static String getResponse(ElephantResponseType type, String message, String code) throws IOException {
72  Gson gson = new GsonBuilder().create();
74  er.type = type.toString();
75  er.message = message;
76  er.code = code;
77  return gson.toJson(er);
78  }
79 
80  public static void writeToResponse(HttpServletResponse response, ElephantResponseType type, String message, String code) throws IOException {
81  response.setContentType("application/json");
82  response.setCharacterEncoding(ElephantContext.getEncoding());
83  response.getWriter().write(getResponse(type, message, code));
84  }
85 }
static String getResponse(ElephantResponseType type, String message, String code)
static ElephantResponse getResponse(HttpEntity entity)
static boolean isCorrect(ElephantResponse er)
static void writeToResponse(HttpServletResponse response, ElephantResponseType type, String message, String code)
static ElephantResponseType getType(ElephantResponse er)