Java Code Examples for org.apache.sling.api.SlingHttpServletResponse#setCharacterEncoding()

The following examples show how to use org.apache.sling.api.SlingHttpServletResponse#setCharacterEncoding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultHtmlServlet.java    From sling-whiteboard with Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

    final Resource r = request.getResource();
    final String resourceType = r.getResourceType();
    final ResourceSchema m = registry.getSchema(resourceType);
    if(m == null) {
        response.sendError(
            HttpServletResponse.SC_BAD_REQUEST,
            "ResourceSchema not found for resource type " + resourceType + " at " + request.getResource().getPath());
        return;
    }
    
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html");

    renderingLogic.render(r, m, new HtmlResourceRenderer(registry, request, response.getWriter()));
    response.getWriter().flush();
}
 
Example 2
Source File: SolrQueryHandler.java    From aem-solr-search with Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");

    JSONArray solrDocs = new JSONArray();

    SolrClient solr = getQueryingSolrClient();

    try {
        LOG.debug("Query using Solr Client {} and the solr core {}", solr, core);
        QueryResponse result = solr.query(core, new SolrQuery("*:*"));
        SolrDocumentList list=result.getResults();
        response.getWriter().write(list.toString());

    } catch (SolrServerException e) {
        e.printStackTrace();
    }


}
 
Example 3
Source File: Two3Servlet3.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
    throws ServletException, IOException {

  LOG.info("Servlet GET Method requested");

  // Write a standard text/html response
  response.setContentType("text/html;charset=UTF-8");
  response.setCharacterEncoding("UTF-8");
  response.getWriter().write("<html><body>Text to write to response</body></html>");
}
 
Example 4
Source File: ErrorHandler.java    From publick-sling-blog with Apache License 2.0 5 votes vote down vote up
@Override
public void activate() {
    SlingHttpServletRequest request = getRequest();
    SlingHttpServletResponse response = getResponse();
    ResourceResolver resolver = getResourceResolver();

    isAnonymous = "anonymous".equals(resolver.adaptTo(Session.class).getUserID());

    message = (String) request.getAttribute(SlingConstants.ERROR_MESSAGE);
    Integer scObject = (Integer) request.getAttribute(SlingConstants.ERROR_STATUS);

    code = (scObject != null) ? scObject.intValue() : response.SC_INTERNAL_SERVER_ERROR;

    if (message == null) {
        message = statusToString(code);
    }

    // Print stack trace only if the user is not anonymous
    if (!isAnonymous) {
        if (request.getAttribute(SlingConstants.ERROR_EXCEPTION) instanceof Throwable) {
            Throwable throwable = (Throwable) request.getAttribute(SlingConstants.ERROR_EXCEPTION);
            printStackTrace(throwable);
        }
    }

    response.setStatus(code);
    response.setContentType("text/html"); 
    response.setCharacterEncoding("utf-8");
}
 
Example 5
Source File: ThreeServlet10.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
    throws ServletException, IOException {

  LOG.info("Servlet GET Method requested");

  // Write a standard text/html response
  response.setContentType("text/html;charset=UTF-8");
  response.setCharacterEncoding("UTF-8");
  response.getWriter().write("<html><body>Text to write to response</body></html>");
}
 
Example 6
Source File: ThreeServlet20.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
    throws ServletException, IOException {

  LOG.info("Servlet GET Method requested");

  // Write a standard text/html response
  response.setContentType("text/html;charset=UTF-8");
  response.setCharacterEncoding("UTF-8");
  response.getWriter().write("<html><body>Text to write to response</body></html>");
}
 
Example 7
Source File: ThreeServlet8.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
    throws ServletException, IOException {

  LOG.info("Servlet GET Method requested");

  // Write a standard text/html response
  response.setContentType("text/html;charset=UTF-8");
  response.setCharacterEncoding("UTF-8");
  response.getWriter().write("<html><body>Text to write to response</body></html>");
}
 
Example 8
Source File: ThreeServlet17.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
    throws ServletException, IOException {

  LOG.info("Servlet GET Method requested");

  // Write a standard text/html response
  response.setContentType("text/html;charset=UTF-8");
  response.setCharacterEncoding("UTF-8");
  response.getWriter().write("<html><body>Text to write to response</body></html>");
}
 
Example 9
Source File: SolrUpdateHandler.java    From aem-solr-search with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");

    Resource page = request.getResource();
    GeometrixxMediaContentType contentType = page.adaptTo(GeometrixxMediaPage.class);

    response.getWriter().write(contentType.getJson().toJSONString());
}
 
Example 10
Source File: Two1Servlet.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
    throws ServletException, IOException {

  LOG.info("Servlet GET Method requested");

  // Write a standard text/html response
  response.setContentType("text/html;charset=UTF-8");
  response.setCharacterEncoding("UTF-8");
  response.getWriter().write("<html><body>Text to write to response</body></html>");
}
 
Example 11
Source File: BackupServlet.java    From publick-sling-blog with Apache License 2.0 5 votes vote down vote up
/**
 * Return all packages on a GET request in order of newest to oldest.
 */
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
        throws ServletException, IOException {

    final PrintWriter writer = response.getWriter();

    response.setCharacterEncoding(CharEncoding.UTF_8);
    response.setContentType("application/json");

    List<JcrPackage> packages = packageService.getPackageList(request);

    try {
        JSONArray jsonArray = new JSONArray();

        for (JcrPackage jcrPackage : packages) {
            final JSONObject json = getJsonFromJcrPackage(jcrPackage);

            jsonArray.put(json);
        }

        response.setStatus(SlingHttpServletResponse.SC_OK);
        writer.write(jsonArray.toString());
    } catch (JSONException | RepositoryException e) {
        LOGGER.error("Could not write JSON", e);
        response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}
 
Example 12
Source File: ThreeServlet14.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
    throws ServletException, IOException {

  LOG.info("Servlet GET Method requested");

  // Write a standard text/html response
  response.setContentType("text/html;charset=UTF-8");
  response.setCharacterEncoding("UTF-8");
  response.getWriter().write("<html><body>Text to write to response</body></html>");
}
 
Example 13
Source File: Two3Servlet.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
    throws ServletException, IOException {

  LOG.info("Servlet GET Method requested");

  // Write a standard text/html response
  response.setContentType("text/html;charset=UTF-8");
  response.setCharacterEncoding("UTF-8");
  response.getWriter().write("<html><body>Text to write to response</body></html>");
}
 
Example 14
Source File: ThreeServlet3.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
    throws ServletException, IOException {

  LOG.info("Servlet GET Method requested");

  // Write a standard text/html response
  response.setContentType("text/html;charset=UTF-8");
  response.setCharacterEncoding("UTF-8");
  response.getWriter().write("<html><body>Text to write to response</body></html>");
}
 
Example 15
Source File: OneServlet.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
@Override
protected final void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
    throws ServletException, IOException {

  LOG.info("Servlet GET Method requested");

  // Write a standard text/html response
  response.setContentType("text/html;charset=UTF-8");
  response.setCharacterEncoding("UTF-8");
  response.getWriter().write("<html><body>Text to write to response</body></html>");
}
 
Example 16
Source File: MimeTypeServlet.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) 
        throws ServletException, IOException {
    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");
    final String mimeType = detector.getMimeType(request.getResource());
    response.getWriter().write(PREFIX); 
    response.getWriter().write(String.valueOf(mimeType));
}
 
Example 17
Source File: RatingPostServlet.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
@Override
protected void doPost(final SlingHttpServletRequest request,
        final SlingHttpServletResponse response)
throws ServletException, IOException {
    final String rating = request.getParameter(RatingsUtil.PROPERTY_RATING);
    final String userId = request.getRemoteUser();

    logger.debug("New rating from {} : {}", userId, rating);

    // save rating
    ResourceResolver resolver = null;
    try {
        resolver = factory.getServiceResourceResolver(null);

        final Resource reqResource = resolver.getResource(request.getResource().getPath());

        ratingsService.setRating(reqResource, userId, Double.valueOf(rating));

    } catch ( final LoginException le ) {
        throw new ServletException("Unable to login", le);
    } finally {
        if ( resolver != null ) {
            resolver.close();
        }
    }

    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");
    response.setStatus(200);

    final PrintWriter pw = response.getWriter();
    pw.print("{ ");
    pw.print(" \"rating\" : ");
    pw.print(String.valueOf(ratingsService.getRating(request.getResource())));
    pw.print("}");
}
 
Example 18
Source File: SolrBulkUpdateHandler.java    From aem-solr-search with Apache License 2.0 4 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");

    JSONArray solrDocs = new JSONArray();

    final String slingResourceType = getSlingResourceType(request);

    if (StringUtils.isEmpty(slingResourceType)) {
        response.getWriter().write(solrDocs.toJSONString());
        return;
    }

    Map<String, String> params = new HashMap<String, String>();
    params.put("path", "/content/geometrixx-media");
    params.put("type", "cq:PageContent");
    params.put("property", "sling:resourceType");
    params.put("property.value", slingResourceType);
    params.put("p.offset", "0");
    params.put("p.limit", "50");

    Session session = null;

    try {
        session = repository.loginAdministrative(null);
        Query query = queryBuilder.createQuery(PredicateGroup.create(params), session);
        SearchResult results = query.getResult();

        LOG.info("Found '{}' matches for query", results.getTotalMatches());

        for (Hit hit: results.getHits()) {

            // The query returns the jcr:content node, so we need its parent.
            Resource page = hit.getResource().getParent();
            GeometrixxMediaContentType contentType = page.adaptTo(GeometrixxMediaPage.class);

            if (contentType != null) {
                solrDocs.add(contentType.getJson());
            }
        }

    } catch (RepositoryException e) {
        LOG.error("Error getting repository", e);
    } finally {
        if (session != null && session.isLive())  {
            session.logout();
        }
    }

    response.getWriter().write(solrDocs.toJSONString());
}
 
Example 19
Source File: EmailConfigServlet.java    From publick-sling-blog with Apache License 2.0 4 votes vote down vote up
/**
 * Save email configuration on POST.
 *
 * @param request The Sling HTTP servlet request.
 * @param response The Sling HTTP servlet response.
 */
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
        throws ServletException, IOException {
    final PrintWriter writer = response.getWriter();
    final boolean allowWrite = userService.isAuthorable(request.getResourceResolver().adaptTo(Session.class));

    response.setCharacterEncoding(CharEncoding.UTF_8);
    response.setContentType("application/json");

    if (allowWrite) {
        final String smtpUsername = request.getParameter(SMTP_USERNAME_PROPERTY);
        final String smtpPassword = request.getParameter(SMTP_PASSWORD_PROPERTY);
        final String sender = request.getParameter(SENDER_PROPERTY);
        final String recipient = request.getParameter(RECIPIENT_PROPERTY);
        final String host = request.getParameter(HOST_PROPERTY);
        final Long port = getPortNumber(request.getParameter(PORT_PROPERTY));

        final Map<String, Object> properties = new HashMap<String, Object>();

        properties.put(EmailService.EMAIL_SMTP_USERNAME, smtpUsername);
        properties.put(EmailService.EMAIL_SENDER, sender);
        properties.put(EmailService.EMAIL_RECIPIENT, recipient);
        properties.put(EmailService.EMAIL_SMTP_HOST, host);
        properties.put(EmailService.EMAIL_SMTP_PORT, port);

        /* Don't save the password if it's all stars. Don't save the password
         * if the user just added text to the end of the stars. This shouldn't
         * happen as the JavaScript should remove the value on focus. Save the
         * password if it's null or blank in order to clear it out. */
        if (smtpPassword == null || !smtpPassword.contains(PublickConstants.PASSWORD_REPLACEMENT)) {
            properties.put(EmailService.EMAIL_SMTP_PASSWORD, smtpPassword);
        }

        final boolean result = emailService.setProperties(properties);

        if (result) {
            response.setStatus(SlingHttpServletResponse.SC_OK);
            sendResponse(writer, "OK", "Settings successfully updated.");
        } else {
            response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            sendResponse(writer, "Error", "Settings failed to update.");
        }
    } else {
        response.setStatus(SlingHttpServletResponse.SC_FORBIDDEN);
        sendResponse(writer, "Error", "Current user not authorized.");
    }
}
 
Example 20
Source File: SimpleJSONServlet.java    From aem-solr-search with Apache License 2.0 3 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
        throws ServletException, IOException {

    PrintWriter out = response.getWriter();

    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");

    solrCore = request.getParameter(REQUEST_PARAM_CORE_NAME);

    getJSONWriter(response.getWriter());
}