Java tutorial
/* Copyright (C) 2010 Brian Dunigan 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.openstatic.http; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.FileInputStream; import java.io.File; import java.net.URL; import java.net.URLConnection; import java.net.FileNameMap; import java.util.Hashtable; import org.json.*; public class HttpResponse { private InputStream data; private String contentType; private String responseCode; private long dataLength; private Hashtable<String, String> headers; public HttpResponse() { this.data = null; this.contentType = "text/html"; this.responseCode = "200 OK"; this.dataLength = 0; this.headers = new Hashtable<String, String>(); } /** Determine the content type of a local file */ public static String getContentTypeFor(String filename) { String lc_file = filename.toLowerCase(); if (lc_file.endsWith(".html") || lc_file.endsWith(".htm")) { return "text/html"; } else if (lc_file.endsWith(".txt")) { return "text/plain"; } else if (lc_file.endsWith(".css")) { return "text/css"; } else if (lc_file.endsWith(".js")) { return "text/javascript"; } else if (lc_file.endsWith(".jpg") || lc_file.endsWith(".jpe") || lc_file.endsWith(".jpeg")) { return "image/jpeg"; } else if (lc_file.endsWith(".gif")) { return "image/gif"; } else { FileNameMap fnm = URLConnection.getFileNameMap(); return fnm.getContentTypeFor(filename); } } /** Set the content type for this response object */ public void setContentType(String type) { this.contentType = type; } /** Set a header for the response */ public void setHeader(String key, String value) { this.headers.put(key, value); } /** Set the entire response body to a single string */ public void setData(String data) { setData(data.getBytes()); } /** Set the entire response body to a string representation of the passed JSONObject */ public void setData(JSONObject data) { setContentType("text/javascript"); setData(data.toString()); } /** Set the entire response body to a string representation of the passed JSONArray */ public void setData(JSONArray data) { setContentType("text/javascript"); setData(data.toString()); } /** Set the response code, this can be "200 OK", "404 Not Found", etc.. */ public void setResponseCode(String value) { this.responseCode = value; } /** Set the entire response body to a string representation of the passed byte array */ public void setData(byte[] data) { this.data = new ByteArrayInputStream(data); this.dataLength = data.length; } /** Set the entire response body to the contents of an InputStream which will be read/relayed until EOF */ public void setData(InputStream is, String contentType) { this.data = is; this.dataLength = -1; this.contentType = contentType; } /** Set the entire response body to the contents of an InputStream which will be read until EOF into memory and then relayed */ public void setBufferedData(InputStream is, String contentType) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int inputByte; while ((inputByte = is.read()) > -1) { baos.write(inputByte); } is.close(); this.data = new ByteArrayInputStream(baos.toByteArray()); this.dataLength = baos.size(); baos.reset(); this.contentType = contentType; } catch (Exception e) { this.responseCode = "404 Not Found"; } } /** Set the entire response body to the resource represented by this URL object, this will be buffered entirely before relay. */ public void setBufferedData(URL url) { try { URLConnection urlc = url.openConnection(); this.setBufferedData(urlc.getInputStream(), urlc.getContentType()); } catch (Exception e) { this.responseCode = "500 Server Error"; } } /** Set the entire response body to the resource represented by this URL object, it will be relayed as read. */ public void setData(URL url) { try { URLConnection urlc = url.openConnection(); this.data = urlc.getInputStream(); this.contentType = urlc.getContentType(); this.dataLength = urlc.getContentLength(); } catch (Exception e) { this.responseCode = "500 Server Error"; } } /** Set the entire response body to the contents of the passed file object, content/type is automatically determined */ public void setData(File file) { try { this.data = new FileInputStream(file); this.contentType = getContentTypeFor(file.toString()); this.dataLength = file.length(); } catch (Exception e) { this.responseCode = "404 Not Found"; } } /** Retrieve an InputStream containing this response's data */ public InputStream getData() { return this.data; } /** Retrieve all headers as a Hashtable */ public Hashtable<String, String> getHeaders() { return this.headers; } /** return the length of this response */ public long getDataLength() { return this.dataLength; } public String getContentType() { return this.contentType; } public String getResponseCode() { return this.responseCode; } }