Untitled

mail@pastecode.io avatar
unknown
java
7 months ago
3.1 kB
23
Indexable
Never
package com.trilha03.core.servlets;

import com.trilha03.core.models.ModelTeste;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.models.factory.ModelFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component(immediate = true, service = javax.servlet.Servlet.class, property = {
        "sling.servlet.methods=" + "POST",
        "sling.servlet.paths=" + "/bin/TesteServlet",
        "sling.servlet.extensions=" + "json"
})
public class TesteServlet extends SlingAllMethodsServlet {

    private static final long serialVersionUID = 1L;
    private static final Logger LOG = LoggerFactory.getLogger(TesteServlet.class);

    @Reference
    private ModelFactory modelFactory;

    @Override
    protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException {
        // Extract data from parameters directly, avoiding reading request body prematurely
        String name = request.getParameter("name");
        String ageParam = request.getParameter("age");

        // Validate age parameter
        int age;
        try {
            age = Integer.parseInt(ageParam);
        } catch (NumberFormatException e) {
            handleBadRequest(response, "Invalid age parameter. Please provide a valid integer.");
            return;
        }

        // Get the current resource, which is assumed to represent an instance of ModelTeste
        Resource resource = request.getResource();

        // Adapt the current resource to ModelTeste using ModelFactory
        ModelTeste modelTeste = modelFactory.createModel(resource, ModelTeste.class);

        // Update ModelTeste properties
        modelTeste.setName(name);
        modelTeste.setAge(age);

        // Commit the changes
        try {
            modelTeste.updateProperties(); 
            LOG.info("AEM node properties updated successfully.");
            response.getWriter().write("AEM node properties updated successfully.");
        } catch (Exception e) {
            LOG.error("Error while updating AEM node properties: {}", e);
            handleServerError(response, "Error while updating AEM node properties.");
        }
    }

    

    private void handleBadRequest(SlingHttpServletResponse response, String message) throws IOException {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        response.getWriter().write(message);
    }

    private void handleServerError(SlingHttpServletResponse response, String message) throws IOException {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        response.getWriter().write(message);
    }
}
Leave a Comment