/* * Created on 25.11.2003 * * (c) Stabsstelle IKT-Strategie des Bundes */ package at.gv.egovernment.moa.ss.erechtclient.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Properties; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import at.gv.egovernment.moa.ss.erechtclient.ERechtClientException; import at.gv.egovernment.moa.ss.erechtclient.init.Constants; /** * @author Gregor Karlinger (mailto:gregor.karlinger@cio.gv.at) */ public class Utils { public static byte[] readFromInputStream(InputStream inputStream) throws IOException { byte[] currentBytes = new byte[500]; int bytesRead; ByteArrayOutputStream result = new ByteArrayOutputStream(); do { bytesRead = inputStream.read(currentBytes); if (bytesRead > 0) { result.write(currentBytes, 0, bytesRead); } } while (bytesRead != -1); return result.toByteArray(); } /* ---------------------------------------------------------------------------------------------------- */ public static void transferStreams(InputStream in, OutputStream out) throws IOException { byte[] currentBytes = new byte[500]; int bytesRead; do { bytesRead = in.read(currentBytes); if (bytesRead > 0) { out.write(currentBytes, 0, bytesRead); } } while (bytesRead != -1); } /* ---------------------------------------------------------------------------------------------------- */ public static void returnErrorPage(HttpServletRequest request, HttpServletResponse response, Throwable t) throws ServletException { Logger logger = Logger.getLogger(Constants.LH_SERVLETS_); // Store Throwable in request context request.setAttribute(Constants.RCP_ERROR_THROWABLE_, t); RequestDispatcher dispatcher = request.getRequestDispatcher(Constants.JSPPN_ERROR_); try { dispatcher.include(request, response); } catch (ServletException e) { logger.error("Returning error JSP page failed.", e); throw e; } catch (IOException e) { String message = "Returning error JSP page failed."; logger.error(message, e); throw new ServletException(message, e); } } /* ---------------------------------------------------------------------------------------------------- */ public static String readInitProperty(Properties initProps, String name, Logger logger) throws ERechtClientException { String value = initProps.getProperty(name); if (value == null) { String message = "Could not read property \"" + name + "\" from configuration properties."; logger.error(message); throw new ERechtClientException(message); } return value; } }