下面列出了org.springframework.web.servlet.ModelAndView#getModel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@RequestMapping("/index")
public ModelAndView toIndex(@RequestBody byte[] bodyParam) {
ModelAndView modelAndView = new ModelAndView("siteMember/siteMember");
Map<String, Object> model = modelAndView.getModel();
PluginProto.ProxyPluginPackage pluginPackage = null;
try {
pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(bodyParam);
Map<Integer, String> headerMap = pluginPackage.getPluginHeaderMap();
String siteUserId = headerMap.get(PluginProto.PluginHeaderKey.CLIENT_SITE_USER_ID_VALUE);
UserProfileBean userProfile = userService.getUserProfile(siteUserId);
model.put("site_user_id", siteUserId);
model.put("site_user_name", userProfile.getUserName());
} catch (InvalidProtocolBufferException e) {
logger.error("to user square error", e);
return new ModelAndView("siteMember/error");
}
return modelAndView;
}
@RequestMapping("/index")
public ModelAndView toIndex(@RequestBody byte[] bodyParam) {
ModelAndView modelAndView = new ModelAndView("siteMember/siteMember");
Map<String, Object> model = modelAndView.getModel();
PluginProto.ProxyPluginPackage pluginPackage = null;
try {
pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(bodyParam);
Map<Integer, String> headerMap = pluginPackage.getPluginHeaderMap();
String siteUserId = headerMap.get(PluginProto.PluginHeaderKey.CLIENT_SITE_USER_ID_VALUE);
UserProfileBean userProfile = userService.getUserProfile(siteUserId);
model.put("site_user_id", siteUserId);
model.put("site_user_name", userProfile.getUserName());
} catch (InvalidProtocolBufferException e) {
logger.error("to user square error", e);
return new ModelAndView("siteMember/error");
}
return modelAndView;
}
protected void postHandle(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod,
VelocityLayout velocityLayout, ModelAndView modelAndView) throws Exception {
if (WebMvcUtils.isPageRenderRequest(modelAndView)) {
Map<String, Object> model = modelAndView.getModel();
if (model.containsKey(layoutKey)) {
if (logger.isDebugEnabled()) {
String message = "Velocity Layout URL[ key : " + layoutKey + "] has been set into Model from HandlerMethod[ " + handlerMethod + " ]";
logger.debug(message);
}
return;
}
model.put(layoutKey, velocityLayout.value());
}
}
@RequestMapping("/index")
public ModelAndView toIndex(@RequestBody byte[] bodyParam) {
ModelAndView modelAndView = new ModelAndView("siteMember/siteMember");
Map<String, Object> model = modelAndView.getModel();
PluginProto.ProxyPluginPackage pluginPackage = null;
try {
pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(bodyParam);
Map<Integer, String> headerMap = pluginPackage.getPluginHeaderMap();
String siteUserId = headerMap.get(PluginProto.PluginHeaderKey.CLIENT_SITE_USER_ID_VALUE);
UserProfileBean userProfile = userService.getUserProfile(siteUserId);
model.put("site_user_id", siteUserId);
model.put("site_user_name", userProfile.getUserName());
} catch (InvalidProtocolBufferException e) {
logger.error("to user square error", e);
return new ModelAndView("siteMember/error");
}
return modelAndView;
}
@Test
public void verifyOK() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(
"GET",
CONTEXT
+ OAuthConstants.CALLBACK_AUTHORIZE_URL);
mockRequest.addParameter(OAuthConstants.TICKET, SERVICE_TICKET);
final MockHttpSession mockSession = new MockHttpSession();
mockSession.putValue(OAuthConstants.OAUTH20_CALLBACKURL, REDIRECT_URI);
mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
mockRequest.setSession(mockSession);
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
oauth20WrapperController.afterPropertiesSet();
final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());
final Map<String, Object> map = modelAndView.getModel();
assertEquals(SERVICE_NAME, map.get("serviceName"));
assertEquals(REDIRECT_URI + "?" + OAuthConstants.CODE + "=" + SERVICE_TICKET, map.get("callbackUrl"));
}
@Test
public void verifyOKWithState() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(
"GET",
CONTEXT
+ OAuthConstants.CALLBACK_AUTHORIZE_URL);
mockRequest.addParameter(OAuthConstants.TICKET, SERVICE_TICKET);
final MockHttpSession mockSession = new MockHttpSession();
mockSession.putValue(OAuthConstants.OAUTH20_CALLBACKURL, REDIRECT_URI);
mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
mockSession.putValue(OAuthConstants.OAUTH20_STATE, STATE);
mockRequest.setSession(mockSession);
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
oauth20WrapperController.afterPropertiesSet();
final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());
final Map<String, Object> map = modelAndView.getModel();
assertEquals(SERVICE_NAME, map.get("serviceName"));
assertEquals(REDIRECT_URI + "?" + OAuthConstants.CODE + "=" + SERVICE_TICKET + "&" + OAuthConstants.STATE + "="
+ STATE, map.get("callbackUrl"));
}
@Test
public void testOK() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(
"GET",
CONTEXT
+ OAuthConstants.CALLBACK_AUTHORIZE_URL);
mockRequest.addParameter(OAuthConstants.TICKET, SERVICE_TICKET);
final MockHttpSession mockSession = new MockHttpSession();
mockSession.putValue(OAuthConstants.OAUTH20_CALLBACKURL, REDIRECT_URI);
mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
mockRequest.setSession(mockSession);
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
oauth20WrapperController.afterPropertiesSet();
final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());
final Map<String, Object> map = modelAndView.getModel();
assertEquals(SERVICE_NAME, map.get("serviceName"));
assertEquals(REDIRECT_URI + "?" + OAuthConstants.CODE + "=" + SERVICE_TICKET, map.get("callbackUrl"));
}
@Test
public void testOKWithState() throws Exception {
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(
"GET",
CONTEXT
+ OAuthConstants.CALLBACK_AUTHORIZE_URL);
mockRequest.addParameter(OAuthConstants.TICKET, SERVICE_TICKET);
final MockHttpSession mockSession = new MockHttpSession();
mockSession.putValue(OAuthConstants.OAUTH20_CALLBACKURL, REDIRECT_URI);
mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
mockSession.putValue(OAuthConstants.OAUTH20_STATE, STATE);
mockRequest.setSession(mockSession);
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
oauth20WrapperController.afterPropertiesSet();
final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());
final Map<String, Object> map = modelAndView.getModel();
assertEquals(SERVICE_NAME, map.get("serviceName"));
assertEquals(REDIRECT_URI + "?" + OAuthConstants.CODE + "=" + SERVICE_TICKET + "&" + OAuthConstants.STATE + "="
+ STATE, map.get("callbackUrl"));
}
/**
* 处理登陆日志
* @param joinPoint
* @param proceed
*/
private void handlerLoginLog(ProceedingJoinPoint joinPoint,Object proceed) {
Signature signature = joinPoint.getSignature();
//是登陆操作
if(signature.getDeclaringTypeName().equals("com.lovecws.mumu.system.controller.system.index.SysLoginController")&&signature.getName().equals("logining")){
//登陆成功
if(proceed instanceof ModelAndView){
ModelAndView modelAndView=(ModelAndView)proceed;
Map<String, Object> model = modelAndView.getModel();
Object code = model.get("code");
if(code!=null&&"200".equals(code.toString())){
messageHandler.handler();
}
}
}
}
@Override
protected ModelAndView doGet(@NotNull HttpServletRequest httpServletRequest, @NotNull HttpServletResponse httpServletResponse) {
ModelAndView modelAndView = new ModelAndView(myPluginDescriptor.getPluginResourcesPath("editProfile.jsp"));
Map<String, Object> model = modelAndView.getModel();
model.put("testConnectionUrl", myPath + "?testConnection=true");
model.put("namespaceChooserUrl", myNamespacesChooser.getUrl());
model.put("deploymentChooserUrl", myDeploymentsChooser.getUrl());
model.put("deleteImageUrl", myKubeDeleteImageDialogController.getUrl());
final String projectId = httpServletRequest.getParameter("projectId");
final List<AgentPool> pools = new ArrayList<>();
if (!BuildProject.ROOT_PROJECT_ID.equals(projectId)){
pools.add(AgentPoolUtil.DUMMY_PROJECT_POOL);
}
pools.addAll(myAgentPoolManager.getProjectOwnedAgentPools(projectId));
model.put("agentPools", pools);
model.put("authStrategies", myAuthStrategyProvider.getAll());
model.put("podTemplateProviders", myPodTemplateProviders.getAll());
return modelAndView;
}
private String render(Resource resource, Map<String, Object> model, Locale locale, EventAndOrganizationId eventAndOrganizationId, TemplateOutput templateOutput) {
try {
ModelAndView mv = new ModelAndView((String) null, model);
mv.addObject("format-date", MustacheCustomTag.FORMAT_DATE);
mv.addObject("country-name", COUNTRY_NAME);
mv.addObject("additional-field-value", ADDITIONAL_FIELD_VALUE.apply(model.get("additional-fields")));
mv.addObject("i18n", new CustomLocalizationMessageInterceptor(locale, messageSourceManager.getMessageSourceForEvent(eventAndOrganizationId)).createTranslator());
var updatedModel = mv.getModel();
updatedModel.putIfAbsent("custom-header-text", "");
updatedModel.putIfAbsent("custom-body-text", "");
updatedModel.putIfAbsent("custom-footer-text", "");
return compile(resource, templateOutput).execute(mv.getModel());
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
/**
* Checks whether the model value under the given {@code modelName}
* exists and checks it type, based on the {@code expectedType}. If the
* model entry exists and the type matches, the model value is returned.
* @param mav the ModelAndView to test against (never {@code null})
* @param modelName name of the object to add to the model (never {@code null})
* @param expectedType expected type of the model value
* @return the model value
*/
@SuppressWarnings("unchecked")
public static <T> T assertAndReturnModelAttributeOfType(ModelAndView mav, String modelName, Class<T> expectedType) {
Map<String, Object> model = mav.getModel();
Object obj = model.get(modelName);
if (obj == null) {
fail("Model attribute with name '" + modelName + "' is null");
}
assertTrue("Model attribute is not of expected type '" + expectedType.getName() + "' but rather of type '" +
obj.getClass().getName() + "'", expectedType.isAssignableFrom(obj.getClass()));
return (T) obj;
}
/**
* Checks whether the model value under the given {@code modelName}
* exists and checks it type, based on the {@code expectedType}. If the
* model entry exists and the type matches, the model value is returned.
* @param mav the ModelAndView to test against (never {@code null})
* @param modelName name of the object to add to the model (never {@code null})
* @param expectedType expected type of the model value
* @return the model value
*/
@SuppressWarnings("unchecked")
public static <T> T assertAndReturnModelAttributeOfType(ModelAndView mav, String modelName, Class<T> expectedType) {
Map<String, Object> model = mav.getModel();
Object obj = model.get(modelName);
if (obj == null) {
fail("Model attribute with name '" + modelName + "' is null");
}
assertTrue("Model attribute is not of expected type '" + expectedType.getName() + "' but rather of type '" +
obj.getClass().getName() + "'", expectedType.isAssignableFrom(obj.getClass()));
return (T) obj;
}
@Test
@SuppressWarnings("unchecked")
void postHandle() {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
User user = mock(User.class);
when(user.isSuperuser()).thenReturn(true);
when(userAccountService.getCurrentUser()).thenReturn(user);
when(appSettings.getLanguageCode()).thenReturn("de");
Object handler = mock(Object.class);
ModelAndView modelAndView = new ModelAndView();
molgenisInterceptor.postHandle(request, response, handler, modelAndView);
Map<String, Object> model = modelAndView.getModel();
assertEquals(resourceFingerprintRegistry, model.get(KEY_RESOURCE_FINGERPRINT_REGISTRY));
Map<String, String> environmentAttributes =
gson.fromJson(String.valueOf(model.get(PluginAttributes.KEY_ENVIRONMENT)), HashMap.class);
assertNotNull(model.get(KEY_APP_SETTINGS));
assertEquals(environment, environmentAttributes.get(ATTRIBUTE_ENVIRONMENT_TYPE));
assertTrue(model.containsKey(PluginAttributes.KEY_I18N));
assertSame(gson, model.get(KEY_GSON));
assertSame("de", model.get(KEY_FALLBACK_LANGUAGE));
assertSame(true, model.get(KEY_SUPER_USER));
}
@Test
void postHandleWithOidcClient() {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
Object handler = mock(Object.class);
ModelAndView modelAndView = new ModelAndView();
OidcClient oidcClient = mock(OidcClient.class);
when(authenticationSettings.getOidcClients()).thenReturn(ImmutableList.of(oidcClient));
when(oidcClient.getRegistrationId()).thenReturn("registrationId");
when(oidcClient.getClientName()).thenReturn("clientName");
User user = mock(User.class);
when(user.isSuperuser()).thenReturn(true);
when(userAccountService.getCurrentUser()).thenReturn(user);
when(appSettings.getLanguageCode()).thenReturn("de");
molgenisInterceptor.postHandle(request, response, handler, modelAndView);
Map<String, Object> model = modelAndView.getModel();
assertEquals(
of(
ImmutableMap.of(
"name",
"clientName",
"registrationId",
"registrationId",
"requestUri",
"/oauth2/authorization/registrationId")),
model.get(KEY_AUTHENTICATION_OIDC_CLIENTS));
}
@RequestMapping("/basicConfig")
public ModelAndView toSiteConfigPage(@RequestBody byte[] bodyParam) {
ModelAndView modelAndView = new ModelAndView("basic/config");
Map<String, Object> model = modelAndView.getModel();
// 设置默认属性
PluginProto.ProxyPluginPackage pluginPackage = null;
try {
pluginPackage = PluginProto.ProxyPluginPackage.parseFrom(bodyParam);
String siteUserId = getRequestSiteUserId(pluginPackage);
if (!isManager(siteUserId)) {
throw new UserPermissionException("Current user is not a manager");
}
if (isAdmin(siteUserId)) {
model.put("manager_type", "admin");
} else if (isManager(siteUserId)) {
model.put("manager_type", "site_manager");
}
model.put("uic_status", "0");
model.put("pic_size", "1");
model.put("pic_path", "/akaxin");
model.put("group_members_count", "100");
model.put("u2_encryption_status", "1");
model.put("push_client_status", "0");
model.put("log_level", "INFO");
Map<Integer, String> map = configManageService.getSiteConfig();
Set<Integer> integers = map.keySet();
String site_prot = "";
String site_address = "";
String http_prot = "";
String http_address = "";
for (Integer integer : integers) {
String res = map.get(integer);
switch (integer) {
case ConfigKey.SITE_NAME_VALUE:
model.put("site_name", res);
break;
case ConfigKey.SITE_ADDRESS_VALUE:
site_address = res;
break;
case ConfigKey.SITE_PORT_VALUE:
site_prot = res;
break;
case ConfigKey.SITE_HTTP_ADDRESS_VALUE:
http_address = res;
break;
case ConfigKey.SITE_HTTP_PORT_VALUE:
http_prot = res;
break;
case ConfigKey.SITE_LOGO_VALUE:
model.put("site_logo", res);
break;
case ConfigKey.SITE_INTRODUCTION_VALUE:
model.put("site_desc", res);
break;
case ConfigKey.REALNAME_STATUS_VALUE:
model.put("realName_status", res);
break;
case ConfigKey.INVITE_CODE_STATUS_VALUE:
model.put("uic_status", res);
break;
case ConfigKey.PIC_SIZE_VALUE:
model.put("pic_size", res);
break;
case ConfigKey.PIC_PATH_VALUE:
model.put("pic_path", res);
break;
case ConfigKey.GROUP_MEMBERS_COUNT_VALUE:
model.put("group_members_count", res);
break;
case ConfigKey.U2_ENCRYPTION_STATUS_VALUE:
model.put("u2_encryption_status", res);
break;
case ConfigKey.PUSH_CLIENT_STATUS_VALUE:
model.put("push_client_status", res);
break;
case ConfigKey.LOG_LEVEL_VALUE:
model.put("log_level", res);
break;
case ConfigKey.SITE_MANAGER_VALUE:
model.put("subgenus_admin", res);
break;
}
}
model.put("siteAddressAndPort", site_address + ":" + site_prot);
model.put("httpAddressAndPort", http_address + ":" + http_prot);
return modelAndView;
} catch (InvalidProtocolBufferException e) {
logger.error("to basic config page error", e);
} catch (UserPermissionException u) {
logger.error("to basic config page error : " + u.getMessage());
}
return new ModelAndView("error");
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
log.info("postHandle....");
Map<String,Object>map=modelAndView.getModel();
map.put("msg","postHandle add msg");
}
@Override
protected void postHandleOnPageRenderContext(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView) throws Exception {
if (!CollectionUtils.isEmpty(toolsMap)) {
Map<String, Object> model = modelAndView.getModel();
model.putAll(toolsMap);
}
}
/**
* Assert whether or not a model attribute is available.
* @param mav the ModelAndView to test against (never {@code null})
* @param modelName name of the object to add to the model (never {@code null})
*/
public static void assertModelAttributeAvailable(ModelAndView mav, String modelName) {
Map<String, Object> model = mav.getModel();
assertTrue("Model attribute with name '" + modelName + "' is not available", model.containsKey(modelName));
}
/**
* Assert whether or not a model attribute is available.
* @param mav the ModelAndView to test against (never {@code null})
* @param modelName name of the object to add to the model (never {@code null})
*/
public static void assertModelAttributeAvailable(ModelAndView mav, String modelName) {
Map<String, Object> model = mav.getModel();
assertTrue("Model attribute with name '" + modelName + "' is not available", model.containsKey(modelName));
}