Skip to content
HomePageController.java 2.05 KiB
Newer Older
Sonia Zorba's avatar
Sonia Zorba committed
package it.inaf.ia2.gms.controller;

import it.inaf.ia2.gms.authn.SessionData;
import it.inaf.ia2.gms.model.request.GroupsRequest;
Sonia Zorba's avatar
Sonia Zorba committed
import it.inaf.ia2.gms.model.response.GroupsTabResponse;
import it.inaf.ia2.gms.model.response.HomePageResponse;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
Sonia Zorba's avatar
Sonia Zorba committed
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
Sonia Zorba's avatar
Sonia Zorba committed

@Controller
public class HomePageController {

    @Autowired
    private SessionData session;

    @Autowired
    private GroupsTabResponseBuilder groupsTabResponseBuilder;

    @ResponseBody
    @GetMapping(value = "/home", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<HomePageResponse> getMainPage(@Valid GroupsRequest request) {
Sonia Zorba's avatar
Sonia Zorba committed

        HomePageResponse response = new HomePageResponse();

        response.setUser(session.getUserName());
Sonia Zorba's avatar
Sonia Zorba committed

        GroupsTabResponse groupsTabResponse = groupsTabResponseBuilder.getGroupsTab(request);
        response.setBreadcrumbs(groupsTabResponse.getBreadcrumbs());
        response.setGroupsPanel(groupsTabResponse.getGroupsPanel());
        response.setPermission(groupsTabResponse.getPermission());

        return ResponseEntity.ok(response);
    }

    @GetMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE)
    public String index() {
        return "index.html";
    }

    @GetMapping(value = "/logout", produces = MediaType.TEXT_HTML_VALUE)
    public void logout(HttpSession httpSession, HttpServletResponse response) throws IOException {
        httpSession.invalidate();
        String baseUrl = ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString();
        response.sendRedirect(baseUrl);
    }
Sonia Zorba's avatar
Sonia Zorba committed
}