org.adaptto.rookie.demo.components.DiscussionComment.java Source code

Java tutorial

Introduction

Here is the source code for org.adaptto.rookie.demo.components.DiscussionComment.java

Source

/*
 * #%L
 * adaptTo()
 * %%
 * Copyright (C) 2014-2015 adaptTo() Conference
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package org.adaptto.rookie.demo.components;

import java.io.IOException;
import java.io.Writer;
import java.text.DateFormat;

import javax.servlet.ServletException;

import org.adaptto.rookie.demo.models.Comment;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

/**
 * Servlet example comment for social comment entry
 */
@SlingServlet(resourceTypes = "/apps/rookiedemo/components/social/comment")
public class DiscussionComment extends SlingSafeMethodsServlet {
    private static final long serialVersionUID = -6549518176129073294L;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws ServletException, IOException {
        Writer out = response.getWriter();

        // read comment via Sling Model
        Comment comment = request.getResource().adaptTo(Comment.class);

        // output comment as HTML
        out.write("<p>");
        out.write("<em>" + escapeHtml(comment.getAuthor()) + " ("
                + DateFormat.getDateTimeInstance().format(comment.getCreated()) + ")</em><br/>");
        out.write(escapeHtml(comment.getText()));
        out.write("</p>");

    }

    private String escapeHtml(String value) {
        return StringEscapeUtils.escapeHtml(StringUtils.defaultString(value));
    }

}