Example usage for javax.servlet.http HttpServletResponse setCharacterEncoding

List of usage examples for javax.servlet.http HttpServletResponse setCharacterEncoding

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse setCharacterEncoding.

Prototype

public void setCharacterEncoding(String charset);

Source Link

Document

Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.

Usage

From source file:com.ucap.uccc.cmis.impl.webservices.CmisWebServicesServlet.java

private void printPage(HttpServletRequest request, HttpServletResponse response, UrlBuilder baseUrl)
        throws ServletException, IOException {
    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentType("text/html");
    response.setCharacterEncoding(IOUtils.UTF8);

    String urlEscaped = StringEscapeUtils
            .escapeHtml((new UrlBuilder(baseUrl)).addPath("cmis").addParameter("wsdl").toString());

    PrintWriter pw = response.getWriter();

    pw.print("<html><head><title>Apache Chemistry OpenCMIS - CMIS " + cmisVersion.value()
            + " Web Services</title>"
            + "<style><!--H1 {font-size:24px;line-height:normal;font-weight:bold;background-color:#f0f0f0;color:#003366;border-bottom:1px solid #3c78b5;padding:2px;} "
            + "BODY {font-family:Verdana,arial,sans-serif;color:black;font-size:14px;} "
            + "HR {color:#3c78b5;height:1px;}--></style></head><body>");
    pw.print("<h1>CMIS " + cmisVersion.value() + " Web Services</h1>");
    pw.print("<p>CMIS WSDL for all services: <a href=\"" + urlEscaped + "\">" + urlEscaped + "</a></p>");

    pw.print("</html></body>");
    pw.flush();//www  .ja  v  a2 s  . c o  m
}

From source file:com.blog.fckeditor.ConnectorServlet.java

/**
 * Manage the <code>GET</code> requests (<code>GetFolders</code>,
 * <code>GetFoldersAndFiles</code>, <code>CreateFolder</code>).<br/>
 * //from  ww  w . ja v a 2 s.  co m
 * The servlet accepts commands sent in the following format:<br/>
 * <code>connector?Command=&lt;CommandName&gt;&Type=&lt;ResourceType&gt;&CurrentFolder=&lt;FolderPath&gt;</code>
 * <p>
 * It executes the commands and then returns the result to the client in XML
 * format.
 * </p>
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    logger.debug("Entering ConnectorServlet#doGet");

    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/xml; charset=UTF-8");
    response.setHeader("Cache-Control", "no-cache");
    PrintWriter out = response.getWriter();

    String commandStr = request.getParameter("Command");
    String typeStr = request.getParameter("Type");
    String currentFolderStr = request.getParameter("CurrentFolder");

    logger.debug("Parameter Command: {}", commandStr);
    logger.debug("Parameter Type: {}", typeStr);
    logger.debug("Parameter CurrentFolder: {}", currentFolderStr);

    XmlResponse xr;

    if (!RequestCycleHandler.isEnabledForFileBrowsing(request))
        xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.NOT_AUTHORIZED_FOR_BROWSING);
    else if (!CommandHandler.isValidForGet(commandStr))
        xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_COMMAND);
    else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr))
        xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_TYPE);
    else if (!UtilsFile.isValidPath(currentFolderStr))
        xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_CURRENT_FOLDER);
    else {
        CommandHandler command = CommandHandler.getCommand(commandStr);
        ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr);

        String typePath = UtilsFile.constructServerSidePath(request, resourceType);
        String typeDirPath = getServletContext().getRealPath(typePath);

        File typeDir = new File(typeDirPath);
        UtilsFile.checkDirAndCreate(typeDir);

        File currentDir = new File(typeDir, currentFolderStr);

        if (!currentDir.exists())
            xr = new XmlResponse(XmlResponse.EN_INVALID_FOLDER_NAME);
        else {

            xr = new XmlResponse(command, resourceType, currentFolderStr, UtilsResponse.constructResponseUrl(
                    request, resourceType, currentFolderStr, true, ConnectorHandler.isFullUrl()));

            if (command.equals(CommandHandler.GET_FOLDERS))
                xr.setFolders(currentDir);
            else if (command.equals(CommandHandler.GET_FOLDERS_AND_FILES))
                xr.setFoldersAndFiles(currentDir);
            else if (command.equals(CommandHandler.CREATE_FOLDER)) {
                String newFolderStr = UtilsFile.sanitizeFolderName(
                        new String(request.getParameter("NewFolderName").getBytes("ISO8859-1"), "UTF-8"));
                logger.debug("Parameter NewFolderName: {}", newFolderStr);

                File newFolder = new File(currentDir, newFolderStr);
                int errorNumber = XmlResponse.EN_UKNOWN;

                if (newFolder.exists())
                    errorNumber = XmlResponse.EN_ALREADY_EXISTS;
                else {
                    try {
                        errorNumber = (newFolder.mkdir()) ? XmlResponse.EN_OK
                                : XmlResponse.EN_INVALID_FOLDER_NAME;
                    } catch (SecurityException e) {
                        errorNumber = XmlResponse.EN_SECURITY_ERROR;
                    }
                }
                xr.setError(errorNumber);
            }
        }
    }

    out.print(xr);
    out.flush();
    out.close();
    logger.debug("Exiting ConnectorServlet#doGet");
}

From source file:dk.itst.oiosaml.sp.service.LogoutServiceSOAPHandler.java

public void handleGet(RequestContext ctx) throws IOException {
    String wsdl = ctx.getRequest().getParameter("wsdl");
    HttpServletResponse response = ctx.getResponse();
    if (wsdl != null) {
        try {/*  www.j a v a 2s.  c  o  m*/
            if (log.isDebugEnabled())
                log.debug("Returning wsdl...");
            PrintWriter out = response.getWriter();
            response.setContentType("text/xml");
            response.setCharacterEncoding("UTF-8");
            InputStream in = LogoutServiceSOAPHandler.class.getResourceAsStream("/SAML2LogoutService.wsdl");
            IOUtils.copy(in, out);

            in.close();
            out.flush();
            return;
        } catch (IOException e) {
            throw new WrappedException(Layer.CLIENT, e);
        }
    } else {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED,
                "No argument wsdl on get request. Use POST for SOAP requests.");
    }
}

From source file:com.ucap.uccc.cmis.impl.webservices.CmisWebServicesServlet.java

private void printXml(HttpServletRequest request, HttpServletResponse response, String doc, UrlBuilder baseUrl)
        throws ServletException, IOException {
    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentType("text/xml");
    response.setCharacterEncoding(IOUtils.UTF8);

    String respDoc = doc;/*w  ww.  j  a v  a2  s .co  m*/
    respDoc = BASE_PATTERN.matcher(respDoc).replaceAll(baseUrl.toString());
    respDoc = CORE_PATTERN.matcher(respDoc)
            .replaceAll((new UrlBuilder(baseUrl)).addPath("cmis").addParameter("core").toString());
    respDoc = MSG_PATTERN.matcher(respDoc)
            .replaceAll((new UrlBuilder(baseUrl)).addPath("cmis").addParameter("msg").toString());

    PrintWriter pw = response.getWriter();
    pw.print(respDoc);
    pw.flush();
}

From source file:com.yeoou.fckeditor.ConnectorServlet.java

/**
 * Manage the <code>GET</code> requests (<code>GetFolders</code>,
 * <code>GetFoldersAndFiles</code>, <code>CreateFolder</code>).<br/>
 * /*  www.jav a2  s  .  com*/
 * The servlet accepts commands sent in the following format:<br/>
 * <code>connector?Command=&lt;CommandName&gt;&Type=&lt;ResourceType&gt;&CurrentFolder=&lt;FolderPath&gt;</code>
 * <p>
 * It executes the commands and then returns the result to the client in XML
 * format.
 * </p>
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    logger.debug("Entering ConnectorServlet#doGet");

    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/xml; charset=UTF-8");
    response.setHeader("Cache-Control", "no-cache");
    PrintWriter out = response.getWriter();

    String commandStr = request.getParameter("Command");
    String typeStr = request.getParameter("Type");
    String currentFolderStr = request.getParameter("CurrentFolder");

    logger.debug("Parameter Command: {}", commandStr);
    logger.debug("Parameter Type: {}", typeStr);
    logger.debug("Parameter CurrentFolder: {}", currentFolderStr);

    XmlResponse xr;

    if (!RequestCycleHandler.isEnabledForFileBrowsing(request))
        xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.NOT_AUTHORIZED_FOR_BROWSING);
    else if (!CommandHandler.isValidForGet(commandStr))
        xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_COMMAND);
    else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr))
        xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_TYPE);
    else if (!UtilsFile.isValidPath(currentFolderStr))
        xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_CURRENT_FOLDER);
    else {
        CommandHandler command = CommandHandler.getCommand(commandStr);
        ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr);

        String typePath = UtilsFile.constructServerSidePath(request, resourceType);
        String typeDirPath = getServletContext().getRealPath(typePath);

        File typeDir = new File(typeDirPath);
        UtilsFile.checkDirAndCreate(typeDir);

        File currentDir = new File(typeDir, currentFolderStr);

        if (!currentDir.exists())
            xr = new XmlResponse(XmlResponse.EN_INVALID_FOLDER_NAME);
        else {

            xr = new XmlResponse(command, resourceType, currentFolderStr, UtilsResponse.constructResponseUrl(
                    request, resourceType, currentFolderStr, true, ConnectorHandler.isFullUrl()));

            if (command.equals(CommandHandler.GET_FOLDERS))
                xr.setFolders(currentDir);
            else if (command.equals(CommandHandler.GET_FOLDERS_AND_FILES))
                xr.setFoldersAndFiles(currentDir);
            else if (command.equals(CommandHandler.CREATE_FOLDER)) {
                String tempName = new String(request.getParameter("NewFolderName").getBytes("ISO8859-1"),
                        "UTF-8");
                String newFolderStr = UtilsFile.sanitizeFolderName(tempName);
                /*String newFolderStr = UtilsFile.sanitizeFolderName(request
                 .getParameter("NewFolderName"));*/

                logger.debug("Parameter NewFolderName: {}", newFolderStr);

                File newFolder = new File(currentDir, newFolderStr);
                int errorNumber = XmlResponse.EN_UKNOWN;

                if (newFolder.exists())
                    errorNumber = XmlResponse.EN_ALREADY_EXISTS;
                else {
                    try {
                        errorNumber = (newFolder.mkdir()) ? XmlResponse.EN_OK
                                : XmlResponse.EN_INVALID_FOLDER_NAME;
                    } catch (SecurityException e) {
                        errorNumber = XmlResponse.EN_SECURITY_ERROR;
                    }
                }
                xr.setError(errorNumber);
            }
        }
    }

    out.print(xr);
    out.flush();
    out.close();
    logger.debug("Exiting ConnectorServlet#doGet");
}

From source file:fi.hoski.web.forms.RaceEntryServlet.java

private void printClassOptions(HttpServletResponse response, BoatInfo boatInfo) throws IOException {
    response.setContentType("application/xhtml+xml");
    response.setCharacterEncoding("UTF-8");
    PrintWriter writer = response.getWriter();
    writer.println("<select>");
    for (String boatClass : boatInfo.getBoatTypes()) {
        writer.println("<option>" + EntityReferences.encode(boatClass, "UTF-8") + "</option>");
    }/*w  ww  .  ja  v a2  s  .  c  om*/
    writer.println("</select>");
    writer.close();
}

From source file:org.cee.highlighter.impl.ContentHighlightHandler.java

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {/*from w w w  .j  a  v  a  2 s .  c o  m*/
        ArticleKey articleKey = getArticleKey(request);
        Article article = articleStore.getArticle(articleKey, false);

        WebResponse webResponse = this.webClient.openWebResponse(new URL(article.getLocation()), true);
        String contentEncoding = webResponse.getContentEncoding();
        response.setCharacterEncoding(contentEncoding);

        Settings settings = DefaultSettings.createDefaultSettings(new URL(article.getLocation()));
        contentHighlighter.highlightContent(response.getWriter(), webResponse, article, settings);
    } catch (StoreException e) {
        throw new ServletException(e);
    } catch (ParserException e) {
        throw new ServletException(e);
    }
}

From source file:org.jetbrains.webdemo.handlers.ServerHandler.java

private void writeResponse(HttpServletRequest request, HttpServletResponse response, String responseBody,
        int statusCode) {
    try {//from   w w w  . j  a  v a 2 s  .  c  o  m
        response.addHeader("Cache-Control", "no-cache");
        response.setCharacterEncoding("UTF-8");
        response.setStatus(statusCode);
        if (!responseBody.equals("")) {
            try (PrintWriter writer = response.getWriter()) {
                writer.write(responseBody);
            }
        }
    } catch (IOException e) {
        //This is an exception we can't send data to client
        ErrorWriter.ERROR_WRITER.writeExceptionToExceptionAnalyzer(e, "UNKNOWN", request.getHeader("Origin"),
                "null");
    }
}

From source file:com.weaforce.system.component.fckeditor.connector.ConnectorServlet.java

/**
 * Manage the <code>GET</code> requests (<code>GetFolders</code>,
 * <code>GetFoldersAndFiles</code>, <code>CreateFolder</code>).<br/>
 * //  ww  w.java 2  s  . com
 * The servlet accepts commands sent in the following format:<br/>
 * <code>connector?Command=&lt;CommandName&gt;&Type=&lt;ResourceType&gt;&CurrentFolder=&lt;FolderPath&gt;</code>
 * <p>
 * It executes the commands and then returns the result to the client in XML
 * format.
 * </p>
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    logger.debug("Entering ConnectorServlet#doGet");

    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/xml; charset=UTF-8");
    response.setHeader("Cache-Control", "no-cache");
    PrintWriter out = response.getWriter();

    String commandStr = request.getParameter("Command");
    String typeStr = request.getParameter("type");
    String currentFolderStr = request.getParameter("CurrentFolder");
    imageSubPath = request.getParameter("subpath");

    logger.info("Parameter Command in doGet: {}", commandStr);
    logger.info("Parameter Type in doGet: {}", typeStr);
    logger.info("Parameter CurrentFolder in doGet: {}", currentFolderStr);

    XmlResponse xr;

    if (!RequestCycleHandler.isEnabledForFileBrowsing(request))
        xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.NOT_AUTHORIZED_FOR_BROWSING);
    else if (!CommandHandler.isValidForGet(commandStr)) {
        xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_COMMAND);
    } else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr))
        xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_TYPE);
    else if (!FileUtils.isValidPath(currentFolderStr))
        xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_CURRENT_FOLDER);
    else {
        CommandHandler command = CommandHandler.getCommand(commandStr);
        ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr);
        // String userLogin = Security.getCurrentUserName()
        // .toLowerCase();
        String typePath = UtilsFile.constructServerSidePath(request, resourceType);
        typePath = typePath + "/" + imageSubPath;
        logger.info("doGet: typePath value is: {}", typePath);
        String typeDirPath = typePath;
        FileUtils.checkAndCreateDir(typeDirPath);
        File typeDir = new File(typeDirPath);

        File currentDir = new File(typeDir, currentFolderStr);

        if (!currentDir.exists())
            xr = new XmlResponse(XmlResponse.EN_INVALID_FOLDER_NAME);
        else {
            String responseUrl = UtilsResponse.constructResponseUrl(resourceType, imageSubPath,
                    currentFolderStr);
            logger.info("responseUrl value in doGet(): {}", responseUrl);
            xr = new XmlResponse(command, resourceType, currentFolderStr, responseUrl);

            if (command.equals(CommandHandler.GET_FOLDERS))
                xr.setFolders(currentDir);
            else if (command.equals(CommandHandler.GET_FOLDERS_AND_FILES))
                xr.setFoldersAndFiles(currentDir);
            else if (command.equals(CommandHandler.CREATE_FOLDER)) {
                String newFolderStr = UtilsFile.sanitizeFolderName(request.getParameter("NewFolderName"));

                File newFolder = new File(currentDir, newFolderStr);
                int errorNumber = XmlResponse.EN_UKNOWN;

                if (newFolder.exists())
                    errorNumber = XmlResponse.EN_ALREADY_EXISTS;
                else {
                    try {
                        errorNumber = (newFolder.mkdir()) ? XmlResponse.EN_OK
                                : XmlResponse.EN_INVALID_FOLDER_NAME;
                    } catch (SecurityException e) {
                        errorNumber = XmlResponse.EN_SECURITY_ERROR;
                    }
                }
                xr.setError(errorNumber);
            }
        }
    }

    out.print(xr);
    out.flush();
    out.close();
    logger.debug("Exiting ConnectorServlet#doGet");
}