Package cz.muni.fi.pa165.ddtroops.web.rest

Source Code of cz.muni.fi.pa165.ddtroops.web.rest.SkillRest

package cz.muni.fi.pa165.ddtroops.web.rest;

import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.fi.pa165.ddtroops.dto.SkillDTO;
import cz.muni.fi.pa165.ddtroops.serviceinterfaces.SkillService;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

/**
*
* @author Svoboda
*/
public class SkillRest extends HttpServlet {

    @Autowired
    @Qualifier("skillWebService")
    private SkillService service;
    private ObjectMapper mapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String path = request.getPathInfo();
        response.setContentType("application/json");

        if (RestHelpers.isAll(path)) {
            mapper.writeValue(response.getOutputStream(), service.getAll());
        } else if (RestHelpers.isNonnegative(RestHelpers.getFirst(path))) {
            mapper.writeValue(response.getOutputStream(), service.getById((long) Integer.parseInt(RestHelpers.getFirst(path))));
        } else {
            RestHelpers.printErrorMessage(response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");

        String path = request.getPathInfo();

        if (RestHelpers.isRoot(path)) {
            try {
                SkillDTO s = mapper.readValue(request.getInputStream(), SkillDTO.class);
                service.create(s);
                response.setHeader("Location", path);
            } catch (IllegalArgumentException | NullPointerException e) {
                RestHelpers.printErrorMessage(response);
            }
        } else {
            RestHelpers.printErrorMessage(response);
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String path = request.getPathInfo();

        if (RestHelpers.isNonnegative(RestHelpers.getFirst(path))) {
            try {
                service.delete(service.getById((long) Integer.parseInt(RestHelpers.getFirst(path))));
            } catch (NullPointerException e) {
                RestHelpers.printErrorMessage(response);
            }
        } else {
            RestHelpers.printErrorMessage(response);
        }
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String path = request.getPathInfo();
        if (RestHelpers.isNonnegative(RestHelpers.getFirst(path))) {
            try {
                SkillDTO s = mapper.readValue(request.getInputStream(), SkillDTO.class);
               
                s.id = (long) Integer.parseInt(RestHelpers.getFirst(path));
               
                service.update(s);
            } catch (IllegalArgumentException | NullPointerException e) {
                RestHelpers.printErrorMessage(response);
            }
        } else {
            RestHelpers.printErrorMessage(response);
        }
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
        super.init(config);
    }
}
TOP

Related Classes of cz.muni.fi.pa165.ddtroops.web.rest.SkillRest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.