0

One of my requirement is to get the session attribute in my one of the setter method so that while setting the data i can play as per my requirement with my data and return new data as expected.

Is it possible to get the HTTP Session Object in my model Class, if not is there any way to achieve this by any other alternative.

Here is my Model Class.

DiscoveredProduct

package com.hcl.ne.model;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

public class DiscoveredProduct {

    private String _id;
    private String _rev;
    private String name;
    private String productCatalogName;
    private String normalizedProductName;
    private String domain;
    private String desc;
    private String product;
    private String company;
    private String dataset;
    private String classType;
    private Map<String,Object> attributes[];
    private List<Map<String,Object>> attributes2;
    private String hostName;
    private String serialNumber;
    private String createdOn;
    private String qulRuleId;
    private String classId;
    private String companyId;
    private String selectedDiscoveredPdt;
    private int pageSize;
    private String isNormalized;
    private String ne_status;  
    private Map<String,Object> normalizedAttributes[];
    private String normalizedOn;



    public String getNe_status() {
        return ne_status;
    }
    public void setNe_status(String ne_status) {
        this.ne_status = ne_status;
    }          


    public String get_id() {
        return _id;
    }
    public void set_id(String _id) {
        this._id = _id;
    }
    public String get_rev() {
        return _rev;
    }
    public void set_rev(String _rev) {
        this._rev = _rev;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDomain() {
        return domain;
    }
    public void setDomain(String domain) {
        this.domain = domain;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    public String getProduct() {
        return product;
    }
    public void setProduct(String product) {
        this.product = product;
    }
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    public String getDataset() {
        return dataset;
    }
    public void setDataset(String dataset) {
        this.dataset = dataset;
    }
    public String getClassType() {
        return classType;
    }
    public void setClassType(String classType) {
        this.classType = classType;
    }
    public String getProductCatalogName() {
        return productCatalogName;
    }
    public void setProductCatalogName(String productCatalogName) {
        this.productCatalogName = productCatalogName;
    }
    public Map<String, Object>[] getAttributes() {
        return attributes;
    }
    public void setAttributes(Map<String, Object>[] attributes) {
        this.attributes = attributes;
    }
    public String getHostName() {
        return hostName;
    }
    public void setHostName(String hostName) {
        this.hostName = hostName;
    }
    public String getSerialNumber() {
        return serialNumber;
    }
    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }
    public String getCreatedOn() {
        return createdOn;
    }
    public void setCreatedOn(String createdOn) {
        HttpSession session = new 
        this.createdOn = createdOn;
    }
    public List<Map<String, Object>> getAttributes2() {
        return attributes2;
    }
    public void setAttributes2(List<Map<String, Object>> attributes2) {
        this.attributes2 = attributes2;
    }

    public String getClassId() {
        return classId;
    }
    public void setClassId(String classId) {
        this.classId = classId;
    }
    public String getCompanyId() {
        return companyId;
    }
    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }
    public String getSelectedDiscoveredPdt() {
        return selectedDiscoveredPdt;
    }
    public void setSelectedDiscoveredPdt(String selectedDiscoveredPdt) {
        this.selectedDiscoveredPdt = selectedDiscoveredPdt;
    }
    public String getQulRuleId() {
        return qulRuleId;
    }
    public void setQulRuleId(String qulRuleId) {
        this.qulRuleId = qulRuleId;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public String getIsNormalized() {
        return isNormalized;
    }
    public void setIsNormalized(String isNormalized) {
        this.isNormalized = isNormalized;

    }
    public String getNormalizedProductName() {
        return normalizedProductName;
    }
    public void setNormalizedProductName(String normalizedProductName) {
        this.normalizedProductName = normalizedProductName;
    }
    public Map<String, Object>[] getNormalizedAttributes() {
        return normalizedAttributes;
    }
    public void setNormalizedAttributes(Map<String, Object>[] normalizedAttributes) {
        this.normalizedAttributes = normalizedAttributes;
    }
    public String getNormalizedOn() {
        return normalizedOn;
    }
    public void setNormalizedOn(String normalizedOn) {
        this.normalizedOn = normalizedOn;
    }


}

Edited Code

package com.hcl.ne.model;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.hcl.ne.service.util.CommonUtility;

public class DiscoveredProduct {

    private String _id;
    private String _rev;
    private String name;
    private String productCatalogName;
    private String normalizedProductName;
    private String domain;
    private String desc;
    private String product;
    private String company;
    private String dataset;
    private String classType;
    private Map<String,Object> attributes[];
    private List<Map<String,Object>> attributes2;
    private String hostName;
    private String serialNumber;
    private String createdOn;
    private String qulRuleId;
    private String classId;
    private String companyId;
    private String selectedDiscoveredPdt;
    private int pageSize;
    private String isNormalized;
    private String ne_status;  
    private Map<String,Object> normalizedAttributes[];
    private String normalizedOn;

    @Autowired
    private HttpSession httpSession;



    public String getNe_status() {
        return ne_status;
    }
    public void setNe_status(String ne_status) {
        this.ne_status = ne_status;
    }          


    public String get_id() {
        return _id;
    }
    public void set_id(String _id) {
        this._id = _id;
    }
    public String get_rev() {
        return _rev;
    }
    public void set_rev(String _rev) {
        this._rev = _rev;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDomain() {
        return domain;
    }
    public void setDomain(String domain) {
        this.domain = domain;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    public String getProduct() {
        return product;
    }
    public void setProduct(String product) {
        this.product = product;
    }
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    public String getDataset() {
        return dataset;
    }
    public void setDataset(String dataset) {
        this.dataset = dataset;
    }
    public String getClassType() {
        return classType;
    }
    public void setClassType(String classType) {
        this.classType = classType;
    }
    public String getProductCatalogName() {
        return productCatalogName;
    }
    public void setProductCatalogName(String productCatalogName) {
        this.productCatalogName = productCatalogName;
    }
    public Map<String, Object>[] getAttributes() {
        return attributes;
    }
    public void setAttributes(Map<String, Object>[] attributes) {
        this.attributes = attributes;
    }
    public String getHostName() {
        return hostName;
    }
    public void setHostName(String hostName) {
        this.hostName = hostName;
    }
    public String getSerialNumber() {
        return serialNumber;
    }
    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }
    public String getCreatedOn() {

            long epochDate = Long.parseLong(createdOn);
            System.out.println(epochDate);
            HttpSession session = getHttpSession();
            String timeZone = (String) session.getAttribute("timeZone");
            String dateFormat = (String) session.getAttribute("dateFormat");
            String offSet = (String) session.getAttribute("offSetValue");
            String resultDate = CommonUtility.getDateInGMT(epochDate, timeZone, dateFormat, offSet);
            return resultDate;


    }
    public void setCreatedOn(String createdOn) {
        this.createdOn = createdOn;
    }
    public List<Map<String, Object>> getAttributes2() {
        return attributes2;
    }
    public void setAttributes2(List<Map<String, Object>> attributes2) {
        this.attributes2 = attributes2;
    }

    public String getClassId() {
        return classId;
    }
    public void setClassId(String classId) {
        this.classId = classId;
    }
    public String getCompanyId() {
        return companyId;
    }
    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }
    public String getSelectedDiscoveredPdt() {
        return selectedDiscoveredPdt;
    }
    public void setSelectedDiscoveredPdt(String selectedDiscoveredPdt) {
        this.selectedDiscoveredPdt = selectedDiscoveredPdt;
    }
    public String getQulRuleId() {
        return qulRuleId;
    }
    public void setQulRuleId(String qulRuleId) {
        this.qulRuleId = qulRuleId;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public String getIsNormalized() {
        return isNormalized;
    }
    public void setIsNormalized(String isNormalized) {
        this.isNormalized = isNormalized;

    }
    public String getNormalizedProductName() {
        return normalizedProductName;
    }
    public void setNormalizedProductName(String normalizedProductName) {
        this.normalizedProductName = normalizedProductName;
    }
    public Map<String, Object>[] getNormalizedAttributes() {
        return normalizedAttributes;
    }
    public void setNormalizedAttributes(Map<String, Object>[] normalizedAttributes) {
        this.normalizedAttributes = normalizedAttributes;
    }
    public String getNormalizedOn() {
        return normalizedOn;
    }
    public void setNormalizedOn(String normalizedOn) {
        this.normalizedOn = normalizedOn;
    }
    public HttpSession getHttpSession() {
        return httpSession;
    }
    public void setHttpSession(HttpSession httpSession) {
        this.httpSession = httpSession;
    }




}
Diwakar Bhatt
  • 495
  • 2
  • 11
  • 24
  • Via [RequestContextHolder](http://stackoverflow.com/questions/1629211/how-do-i-get-the-session-object-in-spring). This article well describe the approach: `public static HttpSession session() { ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); return attr.getRequest().getSession(); } ` – Sergii Getman Oct 19 '16 at 10:45
  • Thanks @SergiiGetman for your response, it really helped. – Diwakar Bhatt Oct 19 '16 at 14:37

1 Answers1

1

Yes it is possible.

  1. Using RequestContextHolder
  2. By Autowiring session.

1.RequestContextHolder

ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession session =  attr.getRequest().getSession();

2.AutoWired

@Autowired private HttpSession httpSession;

Update: No need of getters and setters,autowire and use as httpSession.getAttribute()

Prasanna Kumar H A
  • 3,341
  • 6
  • 24
  • 52
  • thanks @Sam, i got the object but i want to use the session object to get the attribute, while doing so i am getting null pointer exception. I have updated the code. can you please suggest how to get the attribute like we can get in any controller by simply writing session.getAttrubute("any string");. – Diwakar Bhatt Oct 19 '16 at 13:10
  • @diwakar updated the answer...no need of getters and setters just auto wiring is enough... – Prasanna Kumar H A Oct 19 '16 at 13:49
  • However will try the updated answer by you, and will let you know. Thanks @Sam. – Diwakar Bhatt Oct 19 '16 at 14:35
  • @diwaka bhatt that's k...ya let me know the result – Prasanna Kumar H A Oct 19 '16 at 15:13