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

com.buession.web.servlet.http.response.ResponseHeaderProcessor Maven / Gradle / Ivy

There is a newer version: 2.3.3
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to you 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.
 *
 * =================================================================================================
 *
 * This software consists of voluntary contributions made by many individuals on behalf of the
 * Apache Software Foundation. For more information on the Apache Software Foundation, please see
 * .
 *
 * +------------------------------------------------------------------------------------------------+
 * | License: http://www.apache.org/licenses/LICENSE-2.0.txt 										|
 * | Author: Yong.Teng  													|
 * | Copyright @ 2013-2018 Buession.com Inc.														|
 * +------------------------------------------------------------------------------------------------+
 */
package com.buession.web.servlet.http.response;

import com.buession.core.validator.Validate;
import com.buession.web.http.response.ContentType;
import com.buession.web.http.response.ResponseHeader;
import com.buession.web.http.response.ResponseHeaders;
import com.buession.web.servlet.annotation.AbstractProcessor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;

/**
 * @author Yong.Teng
 */
@Aspect
@Component
public class ResponseHeaderProcessor extends AbstractProcessor {

    protected final static String CONTENT_TYPE = "Content-Type";

    protected final static String EXPIRES = "Expires";

    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void responseHeaderProcess(){
    }

    @After("responseHeaderProcess()")
    public void doResponseHeaderProcessAfter(JoinPoint pjp){
        HttpServletResponse response = getHttpServletResponse(pjp);

        if(response == null){
            return;
        }

        Class clazz = pjp.getTarget().getClass();
        Method method = ((MethodSignature) pjp.getSignature()).getMethod();

        if(clazz != null){
            process(clazz, response);
        }

        if(method != null){
            process(method, response);
        }
    }

    private final static void process(final AnnotatedElement annotatedElement, final HttpServletResponse response){
        if(AnnotatedElementUtils.hasAnnotation(annotatedElement, ResponseHeader.class)){
            setHeader(response, AnnotatedElementUtils.findMergedAnnotation(annotatedElement, ResponseHeader.class));
        }

        if(AnnotatedElementUtils.hasAnnotation(annotatedElement, ResponseHeaders.class)){
            setHeaders(response, AnnotatedElementUtils.findMergedAnnotation(annotatedElement, ResponseHeaders.class));
        }

        if(AnnotatedElementUtils.hasAnnotation(annotatedElement, ContentType.class)){
            setContentType(response, AnnotatedElementUtils.findMergedAnnotation(annotatedElement, ContentType.class));
        }
    }

    private final static void setHeader(final HttpServletResponse response, final String name, final String value){
        if(EXPIRES.equalsIgnoreCase(name) == true){
            ResponseUtils.httpCache(response, Integer.parseInt(value));
        }else{
            response.addHeader(name, value);
        }
    }

    private final static void setHeader(final HttpServletResponse response, final ResponseHeader responseHeader){
        setHeader(response, responseHeader.name(), responseHeader.value());
    }

    private final static void setHeaders(final HttpServletResponse response, final ResponseHeaders responseHeaders){
        ResponseHeader[] headers = responseHeaders.value();

        if(Validate.isEmpty(headers) == false){
            for(ResponseHeader header : headers){
                setHeader(response, header);
            }
        }
    }

    private final static void setContentType(final HttpServletResponse response, final ContentType contentType){
        StringBuffer sb = new StringBuffer();

        sb.append(contentType.mime());

        if(Validate.hasText(contentType.encoding()) == false){
            sb.append("; charset=");
            sb.append(contentType.encoding());
        }

        setHeader(response, CONTENT_TYPE, sb.toString());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy