All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cattleframework.webmvc.view.HtmlPageView Maven / Gradle / Ivy

There is a newer version: 1.0.1-M3
Show newest version
/*
 * Copyright (C) 2018 the original author or authors.
 *
 * 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.cattleframework.webmvc.view;

import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.cattleframework.aop.context.SpringContext;
import org.cattleframework.aop.resource.Resource;
import org.cattleframework.utils.auxiliary.StreamUtils;
import org.cattleframework.utils.auxiliary.StringTemplate;
import org.springframework.web.servlet.View;
import org.springframework.web.util.ContentCachingResponseWrapper;
import org.springframework.web.util.WebUtils;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * Html页面视图
 * 
 * @author orange
 *
 */
public class HtmlPageView implements View {

    private String encodingScheme;

    private Resource resource;

    public HtmlPageView(Resource resource) {
	this.resource = resource;
    }

    public void setEncodingScheme(String encodingScheme) {
	this.encodingScheme = encodingScheme;
    }

    @Override
    public void render(Map model, HttpServletRequest request, HttpServletResponse response)
	    throws Exception {
	String enc = encodingScheme;
	if (enc == null) {
	    enc = request.getCharacterEncoding();
	}
	if (enc == null) {
	    enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
	}
	String htmlContent;
	try (InputStream is = resource.getInputStream()) {
	    htmlContent = StreamUtils.inputStream2String(is, enc);
	}
	if (StringUtils.isNotEmpty(htmlContent)) {
	    int pos = htmlContent.indexOf("");
	    if (pos >= 0) {
		htmlContent = htmlContent.substring(pos);
	    }
	}
	StringTemplate st = new StringTemplate(htmlContent, StringTemplate.REGEX_PATTERN_JAVA_STYLE);
	Arrays.stream(st.getVariables()).forEach(variable -> {
	    if (StringUtils.isNotBlank(variable)) {
		String[] variableArray = StringUtils.splitByWholeSeparator(variable, ":");
		String variableName = StringUtils.trim(variableArray[0]);
		String variableDefault = variableArray.length > 1 ? StringUtils.trim(variableArray[1]) : null;
		String variableValue;
		if (model.containsKey(variableName)) {
		    variableValue = model.get(variableName).toString();
		} else {
		    variableValue = SpringContext.get().getEnvironment().getProperty(variableName, variableDefault);
		}
		st.setVariable(variable, variableValue);
	    }
	});
	ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
	responseWrapper.setCharacterEncoding(enc);
	responseWrapper.setContentType("text/html;charset=" + enc);
	responseWrapper.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
	responseWrapper.setHeader("Pragma", "no-cache");
	responseWrapper.setDateHeader("Expires", 0);
	try (PrintWriter printWriter = responseWrapper.getWriter()) {
	    printWriter.write(st.getParseResult());
	}
	responseWrapper.copyBodyToResponse();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy