com.nec.harvest.provider.PageProvider.java Source code

Java tutorial

Introduction

Here is the source code for com.nec.harvest.provider.PageProvider.java

Source

/*
 * Copyright(C) 2014
 * NEC Corporation All rights reserved.
 * 
 * No permission to use, copy, modify and distribute this software
 * and its documentation for any purpose is granted.
 * This software is provided under applicable license agreement only.
 */
package com.nec.harvest.provider;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;

import com.nec.core.container.ContextAwareContainer;
import com.nec.harvest.helper.ProductHelper;

/**
 * @author sondn
 *
 */
public class PageProvider {

    private static final String QUESTION_CHARACTER = "?";
    private static final String EQUALS_CHARACTER = "=";

    private static final String REVISION_PARAMS = "r";

    /**
     * Get relative URL for every resources
     * 
     * @param relativeUrl
     * @return
     * @throws IOException
     */
    public String relativeUrl(String relativeUrl) throws IOException {
        relativeUrl = getRequest().getContextPath() + relativeUrl;

        // Add version on Url
        return addVersionOnUrl(relativeUrl);
    }

    /**
     * Get a instance of HttpServletRequest
     * 
     * @return HttpServletRequest
     */
    protected HttpServletRequest getRequest() {
        return ContextAwareContainer.getInstance().getRequest();
    }

    /**
     * Get relative URL for every resources with product version
     * 
     * @param relativeUrl
     * @return
     * @throws IOException
     */
    protected String addVersionOnUrl(String relativeUrl) throws IOException {
        String version;

        try {
            version = ProductHelper.getProductInfor().getVersion();
        } catch (Exception ex) {
            version = ProductHelper.getProductInfor().getProjectVersion();
        }

        relativeUrl += QUESTION_CHARACTER;
        relativeUrl += REVISION_PARAMS;
        relativeUrl += EQUALS_CHARACTER;
        relativeUrl += DigestUtils.md5Hex(version);
        return relativeUrl;
    }
}