HtmlController.java
/**
* Copyright 2014 Global Crop Diversity Trust
*
* 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.
**/
package org.genesys.server.mvc;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.genesys.server.servlet.filter.LocaleURLFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ResolvableType;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Controller which simply handles *.html requests
*/
@Controller
public class HtmlController extends BaseController {
private static String authorizationRequestBaseUri = "oauth2/authorization";
@Autowired
private ClientRegistrationRepository clientRegistrationRepository;
@RequestMapping("/")
public String index(HttpServletRequest request) {
Locale requestLocale = request.getLocale();
Locale requestedLocale = (Locale) request.getAttribute(LocaleURLFilter.REQUEST_LOCALE_ATTR);
if (LOG.isDebugEnabled()) {
LOG.debug("Request locale: {} requested: {}", requestLocale, requestedLocale);
}
if (requestedLocale == null && requestLocale != null) {
LOG.info("Redirecting to request locale: {}", requestLocale.getLanguage());
return "redirect:/" + requestLocale.getLanguage().toLowerCase() + "/welcome";
}
return "redirect:/welcome";
}
@RequestMapping(value = "welcome")
public String welcome(ModelMap model) {
return "/index";
}
@RequestMapping(value = "/login")
public String login(final ModelMap model) {
Map<String, String> oauth2AuthenticationUrls = new HashMap<>();
Iterable<ClientRegistration> clientRegistrations = null;
ResolvableType type = ResolvableType.forInstance(clientRegistrationRepository).as(Iterable.class);
if (type != ResolvableType.NONE &&
ClientRegistration.class.isAssignableFrom(type.resolveGenerics()[0])) {
clientRegistrations = (Iterable<ClientRegistration>) clientRegistrationRepository;
clientRegistrations.forEach(registration -> {
// Add all but self!
if (! registration.getRegistrationId().equals("local")) {
oauth2AuthenticationUrls.put(
registration.getClientName(),
authorizationRequestBaseUri + "/" + registration.getRegistrationId()
);
}
});
}
model.addAttribute("urls", oauth2AuthenticationUrls);
return "/login";
}
}