Example usage for java.io PrintWriter write

List of usage examples for java.io PrintWriter write

Introduction

In this page you can find the example usage for java.io PrintWriter write.

Prototype

public void write(String s) 

Source Link

Document

Writes a string.

Usage

From source file:be.e_contract.eid.android.endpoint.AndroidEndpointServlet.java

@Override
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletresponse)
        throws ServletException, IOException {
    LOG.debug("doPost");
    BeIDOperation operation = getOperation();
    if (BeIDOperation.READ_IDENTITY == operation) {
        Response response = new Response();
        response.action = "identity";
        PrintWriter printWriter = httpServletresponse.getWriter();
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();//from  w  w  w . j a  va  2  s  . c o  m
        printWriter.write(gson.toJson(response));
    }
}

From source file:jp.aegif.liferay.experiment.mryoshio.rr.RoomPortlet.java

@Override
public void serveResource(ResourceRequest req, ResourceResponse res) throws IOException, PortletException {

    if (logger.isDebugEnabled())
        debug(req);//from  ww  w .  ja v  a2 s.co  m

    try {
        String rid = req.getResourceID();
        PortletBag pbag = PortletBagPool.get(PortalUtil.getPortletId(req));
        String ajaxResponse = null;

        if ("add-view".equals(rid) || "update-view".equals(rid)) {
            ajaxResponse = PortalUtil.renderPage(pbag.getServletContext(),
                    PortalUtil.getHttpServletRequest(req), PortalUtil.getHttpServletResponse(res), editJSP,
                    false);
        } else if ("list-view".equals(rid)) {
            ajaxResponse = getListDataAsJson(Integer.parseInt(req.getParameter("itemPerPage")),
                    Integer.parseInt(req.getParameter("targetPage")));
        } else if ("update-room".equals(rid)) {
            updateRoom(req);
        } else if ("delete-room".equals(rid)) {
            deleteRoom(req);
        } else {
            ajaxResponse = ERR_NO_TYPE_SPECIFIED;
        }
        PrintWriter writer = res.getWriter();
        writer.write(ajaxResponse);
        writer.flush();
        writer.close();
    } catch (ServletException e) {
        throw new PortletException(e);
    }
}

From source file:net.sourceforge.jwebunit.tests.util.ParamsServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.write(HtmlHelper.getStart("Submitted parameters"));
    out.write("<h1>Submitted parameters</h1>\n<p>Params are:<br/>");
    /*/*ww  w  .  j  a  va2 s  .  co m*/
     * Prints POST and GET parameters as name=[value1[,value2...]] separated
     * by <BR/>
     */

    // Check that we have a file upload request
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        // Create a factory for disk-based file items
        FileItemFactory factory = new DiskFileItemFactory();

        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);

        // Parse the request
        List /* FileItem */ items = null;
        try {
            items = upload.parseRequest(request);
        } catch (FileUploadException e) {
            throw new ServletException(e);
        }

        String ref = null;
        // Process the uploaded items
        Iterator iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();

            if (item.isFormField()) {
                out.write(" " + item.getFieldName() + "=[" + item.getString());
                if (item.getFieldName().equals("myReferer")) {
                    ref = item.getString();
                }
            } else {
                String fieldName = item.getFieldName();
                String fileName = item.getName();
                out.write(" " + fieldName + "=[" + fileName + "{" + new String(item.get()) + "}");

            }
            if (iter.hasNext()) {
                out.write("]<br/>\n");
            }
        }
        out.write("]</p>\n");
        out.write(HtmlHelper.getLinkParagraph("return", ref));
    } else {
        java.util.Enumeration params = request.getParameterNames();
        for (; params.hasMoreElements();) {
            String p = params.nextElement().toString();
            String[] v = request.getParameterValues(p);
            out.write(p + "=[");
            int n = v.length;
            if (n > 0) {
                out.write(v[0] != null ? v[0] : "");
                for (int i = 1; i < n; i++) {
                    out.write("," + (v[i] != null ? v[i] : ""));
                }
            }
            if (params.hasMoreElements()) {
                out.write("]<br/>\n");
            }
        }
        out.write("]</p>\n");
        String ref = request.getHeader("Referer");
        if (ref == null) {
            if (request.getParameterValues("myReferer") != null) {
                ref = request.getParameterValues("myReferer")[0];
            }
        }
        out.write(HtmlHelper.getLinkParagraph("return", ref));
    }
    out.write(HtmlHelper.getEnd());
}

From source file:de.ingrid.server.opensearch.servlet.OpensearchServerServlet.java

/**
 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 *//*from   ww w .ja  va  2 s  .  com*/
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {
    IngridHits hits = null;
    long overallStartTime = 0;

    if (log.isDebugEnabled()) {
        overallStartTime = System.currentTimeMillis();
    }

    final RequestWrapper reqWrapper = new RequestWrapper(request);

    // set content Type
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/xml");
    response.setCharacterEncoding("UTF-8");

    OSSearcher osSearcher = OSSearcher.getInstance();

    if (osSearcher == null) {
        // redirect or show error page or empty result
        final PrintWriter pout = response.getWriter();
        pout.write("Error: no index file found");
        return;
    }

    int page = reqWrapper.getRequestedPage();
    final int hitsPerPage = reqWrapper.getHitsPerPage();
    if (page <= 0) {
        page = 1;
    }
    final int startHit = (page - 1) * hitsPerPage;

    try {
        // Hits also need Details which has title and summary!!!
        hits = osSearcher.searchAndDetails(reqWrapper.getQuery(), startHit, reqWrapper.getHitsPerPage());
    } catch (final Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // transform hits into target format
    final OpensearchMapper mapper = (new SpringUtil("spring/spring.xml")).getBean("mapper",
            OpensearchMapper.class);
    final HashMap<String, Object> parameter = new HashMap<String, Object>();
    parameter.put(OpensearchMapper.REQUEST_WRAPPER, reqWrapper);

    final Document doc = mapper.mapToXML(hits, parameter);

    // write target format
    final PrintWriter pout = response.getWriter();

    pout.write(doc.asXML());
    pout.close();
    request.getInputStream().close();
    doc.clearContent();

    if (log.isDebugEnabled()) {
        log.debug("Time for complete search: " + (System.currentTimeMillis() - overallStartTime) + " ms");
    }
}

From source file:com.adobe.acs.commons.quickly.impl.QuicklyFilter.java

@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
        final FilterChain filterChain) throws IOException, ServletException {

    final HttpServletRequest request = (HttpServletRequest) servletRequest;
    final HttpServletResponse response = (HttpServletResponse) servletResponse;

    if (!accepts(request)) {
        filterChain.doFilter(servletRequest, servletResponse);
        return;/*w ww .  j av a  2  s  . com*/
    }

    final BufferingResponse capturedResponse = new BufferingResponse(response);

    filterChain.doFilter(request, capturedResponse);

    // Get contents
    final String contents = capturedResponse.getContents();

    if (contents != null && StringUtils.contains(response.getContentType(), "html")) {

        final int bodyIndex = contents.indexOf("</body>");
        if (bodyIndex != -1) {

            final PrintWriter printWriter = response.getWriter();

            printWriter.write(contents.substring(0, bodyIndex));
            printWriter.write(appHTML);
            printWriter.write(contents.substring(bodyIndex));

            return;
        }
    }

    if (contents != null) {
        response.getWriter().write(contents);
    }
}

From source file:de.iew.web.view.js.RequireJSMessageBundleView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> stringObjectMap, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    response.setContentType("text/javascript; charset=utf-8");

    PrintWriter writer = response.getWriter();

    // Ja, Javascript Hlle :-)
    writer.write("define({");
    if (isRootBundle(stringObjectMap)) {
        writer.write("'root':{");
    }/*from  www  .j av  a 2s. co  m*/

    Map<String, String> bundle = getBundle(stringObjectMap);
    int itemsLeft = bundle.size();
    for (Map.Entry<String, String> item : bundle.entrySet()) {
        writer.write("'");
        writer.write(item.getKey());
        writer.write("':'");
        writer.write(item.getValue());
        writer.write("'");
        itemsLeft--;

        if (itemsLeft > 0) {
            writer.write(",");
        }
    }

    if (isRootBundle(stringObjectMap)) {
        writer.write("}");

        List<Locale> availableLocales = getAvailableLocales(stringObjectMap);
        for (Locale locale : availableLocales) {
            if (!locale.equals(this.defaultLocale)) {
                writer.write(",");
                writer.write("'");
                writer.write(locale.getLanguage().toLowerCase() + "-" + locale.getCountry().toLowerCase());
                writer.write("':");
                writer.write("true");
            }
        }
    }
    writer.write("});");
}

From source file:de.mpg.escidoc.pubman.sword.PubManServiceDocumentServlet.java

/**
 * Process the GET request./*w w  w.  j av a  2s . c  o  m*/
 * @param HttpServletRequest
 * @param HttpServletResponse
 * @throws ServletException
 * @throws IOException
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // Create the ServiceDocumentRequest
    ServiceDocumentRequest sdr = new ServiceDocumentRequest();
    SwordUtil util = new SwordUtil();
    AccountUserVO user = null;

    String usernamePassword = this.getUsernamePassword(request);
    if ((usernamePassword != null) && (!usernamePassword.equals(""))) {
        int p = usernamePassword.indexOf(":");
        if (p != -1) {
            sdr.setUsername(usernamePassword.substring(0, p));
            sdr.setPassword(usernamePassword.substring(p + 1));
            user = util.getAccountUser(sdr.getUsername(), sdr.getPassword());
            this.currentUser = user;
        }
    } else {
        String s = "Basic realm=\"SWORD\"";
        response.setHeader("WWW-Authenticate", s);
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        return;
    }

    try {
        String doc = this.swordServer.doServiceDocument(sdr);
        this.currentUser = null;

        // Print out the Service Document
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/xml");
        PrintWriter out = response.getWriter();
        out.write(doc);
        out.flush();
    } catch (SWORDAuthenticationException sae) {
        response.setHeader("WWW-Authenticate", sae.getLocalizedMessage());
        response.setStatus(401);
        response.setCharacterEncoding("UTF-8");
        this.currentUser = null;
    } catch (Exception e) {
        this.log.error(e);
    }
}

From source file:com.yahoo.glimmer.web.JsObjectView.java

@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    Object object = model.get(QueryController.OBJECT_KEY);
    if (object == null) {
        throw new IllegalArgumentException("Model does not contain an object!");
    }/* w ww  .ja va 2 s.  c o m*/

    response.setContentType(getContentType());
    PrintWriter writer = response.getWriter();

    String callback = request.getParameter(CALLBACK_PARAMETER);
    if (callback != null) {
        writer.write(callback);
        writer.write('(');
        writer.write(gson.toJson(object));
        writer.write(");");
    } else {
        writer.write(gson.toJson(object));
    }
}

From source file:org.cloudfoundry.samples.handson.ex4.EnvVariablesController.java

/**
 * Use the out variable to dump the contents of the environment variables.
 * //from   w ww .  j av  a2 s  . com
 * Notice how CloudFoundry instructs the app about services it provides
 * inside variables named {@code VCAP_something}.
 */
@RequestMapping("/ex4")
public void dump(HttpServletResponse response) throws Exception {
    @SuppressWarnings("unused")
    PrintWriter out = response.getWriter();

    Map<String, String> env = System.getenv();
    for (Entry<String, String> envi : env.entrySet()) {
        out.write(envi.getKey() + " => " + envi.getValue() + "\n");
    }

    // ...
}

From source file:jp.aegif.liferay.experiment.mryoshio.rr.ReservationPortlet.java

@Override
public void serveResource(ResourceRequest req, ResourceResponse res) throws IOException, PortletException {

    if (logger.isDebugEnabled())
        debug(req);//from   www . j  a v a  2s.  c  o  m

    try {
        String rid = req.getResourceID();
        PortletBag pbag = PortletBagPool.get(PortalUtil.getPortletId(req));
        String ajaxResponse = null;
        if ("add-view".equals(rid) || "update-view".equals(rid)) {
            ajaxResponse = PortalUtil.renderPage(pbag.getServletContext(),
                    PortalUtil.getHttpServletRequest(req), PortalUtil.getHttpServletResponse(res), editJSP,
                    false);
        } else if ("list-view".equals(rid)) {
            ajaxResponse = getListDataAsJson(Integer.parseInt(req.getParameter("itemPerPage")),
                    Integer.parseInt(req.getParameter("targetPage")));
        } else if ("update-reservation".equals(rid)) {
            updateReservation(req);
        } else if ("delete-reservation".equals(rid)) {
            deleteReservation(req);
        } else {
            ajaxResponse = ERR_NO_TYPE_SPECIFIED;
        }
        PrintWriter writer = res.getWriter();
        writer.write(ajaxResponse);
        writer.flush();
        writer.close();
    } catch (ServletException e) {
        throw new PortletException(e);
    }
}