<commit_before>package org.esupportail.smsuapiadmin.web.controllers;

import java.io.IOException;
import java.net.URI;

import javax.ws.rs.core.Response;

import javax.inject.Inject;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.map.ObjectMapper;
import org.esupportail.smsuapiadmin.business.UserManager;
import org.esupportail.smsuapiadmin.dto.beans.UIUser;
import org.esupportail.smsuapiadmin.web.AuthAndRoleAndMiscFilter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.esupportail.smsu.services.UrlGenerator;

@RequestMapping(value = "/login")
public class LoginController {
	
    @Inject private UserManager userManager;
    @Inject private UrlGenerator urlGenerator;
    
	@RequestMapping(method = RequestMethod.GET)
    public Response get(HttpServletRequest request) throws IOException {
    	boolean ourCookiesRejected = !hasCookie(request, "JSESSIONID");

    HttpSession session = request.getSession();
	String sessionId = ourCookiesRejected ? session.getId() : null;
	String idpId = AuthAndRoleAndMiscFilter.getIdpId(request);
	String then = request.getParameter("then");
	if (then != null) {
		//then = URLDecoder.decode(then, "UTF-8");
		String url = urlGenerator.goTo(request, then, sessionId, idpId);
		return Response.temporaryRedirect(URI.create(url)).build();		
	}

	UIUser user = userManager.getUserByLogin(request.getRemoteUser());
	user.idpId = idpId;
	if (ourCookiesRejected) {
		user.sessionId = session.getId();
	}
	String jsUser = new ObjectMapper().writeValueAsString(user);
	String content, type;
	if (request.getParameter("postMessage") != null) {
		type = "text/html";
		content = "Login success, please wait...\n<script>\n (window.opener ? (window.opener.postMessage ? window.opener : window.opener.document) : window.parent).postMessage('loggedUser=' + JSON.stringify(" + jsUser + "), '*');\n</script>";
	} else if (request.getParameter("callback") != null) {
		type = "application/x-javascript";
		content = request.getParameter("callback") + "(" + jsUser + ")";
	} else {
		type = "application/json";
		content = jsUser;
	}
        return Response.status(Response.Status.OK).type(type).entity(content).build();
    }

	// call this function on successful login
    // if we managed to get here and there is no cookie, it means they have been rejected
    public static boolean hasCookie(HttpServletRequest request, String name) {
    	for (Cookie cookie : request.getCookies()) {
    		if (cookie.getName().equals(name))
    			// cool, our previous Set-Cookie was accepted
    			return true;
    	}
    	return false;
    }

}<commit_msg>WIP Substitution of javax.ws.rs.core.Response by org.springframework.http.ResponseEntity.<commit_after>package org.esupportail.smsuapiadmin.web.controllers;

import java.io.IOException;
import java.net.URI;

import javax.inject.Inject;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.map.ObjectMapper;
import org.esupportail.smsuapiadmin.business.UserManager;
import org.esupportail.smsuapiadmin.dto.beans.UIUser;
import org.esupportail.smsuapiadmin.web.AuthAndRoleAndMiscFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.esupportail.smsu.services.UrlGenerator;

@RequestMapping(value = "/login")
public class LoginController {
	
    @Inject private UserManager userManager;
    @Inject private UrlGenerator urlGenerator;
    
	@RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<String> get(HttpServletRequest request) throws IOException {
    	boolean ourCookiesRejected = !hasCookie(request, "JSESSIONID");

    HttpSession session = request.getSession();
	String sessionId = ourCookiesRejected ? session.getId() : null;
	String idpId = AuthAndRoleAndMiscFilter.getIdpId(request);
	String then = request.getParameter("then");
	if (then != null) {
		//then = URLDecoder.decode(then, "UTF-8");
		String url = urlGenerator.goTo(request, then, sessionId, idpId);
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.setLocation(URI.create(url));
		return new ResponseEntity<String>("", responseHeaders, HttpStatus.TEMPORARY_REDIRECT);
	}

	UIUser user = userManager.getUserByLogin(request.getRemoteUser());
	user.idpId = idpId;
	if (ourCookiesRejected) {
		user.sessionId = session.getId();
	}
	String jsUser = new ObjectMapper().writeValueAsString(user);
	String content;
	MediaType mediaType;
	if (request.getParameter("postMessage") != null) {
		mediaType = MediaType.TEXT_HTML;
		content = "Login success, please wait...\n<script>\n (window.opener ? (window.opener.postMessage ? window.opener : window.opener.document) : window.parent).postMessage('loggedUser=' + JSON.stringify(" + jsUser + "), '*');\n</script>";
	} else if (request.getParameter("callback") != null) {
		mediaType = MediaType.parseMediaType("application/x-javascript");
		content = request.getParameter("callback") + "(" + jsUser + ")";
	} else {
		mediaType = MediaType.APPLICATION_JSON;
		content = jsUser;
	}
        HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.setContentType(mediaType);
        return new ResponseEntity<String>(content, responseHeaders, HttpStatus.OK);
    }

	// call this function on successful login
    // if we managed to get here and there is no cookie, it means they have been rejected
    public static boolean hasCookie(HttpServletRequest request, String name) {
    	for (Cookie cookie : request.getCookies()) {
    		if (cookie.getName().equals(name))
    			// cool, our previous Set-Cookie was accepted
    			return true;
    	}
    	return false;
    }

}
{'fraction_non_alphanumeric': 0.1194522356046857, 'fraction_numerical': 0.00032997855139415937, 'mean_word_length': 4.896887159533074, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6349260', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1820, 'n_words': 386}