Session atrribute is missing a List - java

I have a very strange situation. I´m working on a pretty big Java application with many bugs, and I found this one today.
I´ll try to explain the situation without posting code because the methods are way too long and I have identified and isolated the specific problem. Here it is:
I have a session attribute set on a Controller class. The attribute has several fields, a couple of Strings, a couple of int and an ArrayList of a certain object type. It is set like this:
request.getSession().setAttribute(Constants.SESSION_LIST_SEARCH, beanList);
Then there is another Controller class where I need to read this attribute, it goes like this:
request.getSession().getAttribute(Constants.SESSION_LIST_SEARCH);
When the controller gets the attribute (with the proper cast) the Strings and the int fields are there, but the ArrayList it´s empty.
I couldn't find an answer so in an act of desperation I tried to "clone" the list to see what happened, so it goes like this:
request.getSession().setAttribute(Constants.SESSION_LIST_SEARCH, beanList);
/* Desperate developer */
ArrayList<ActivityBean> duplicatedList = new ArrayList<ActivityBean>();
for(ActivityBean foo:beanList.getActivityBean()){
duplicatedList.add(foo);
}
request.getSession().setAttribute("duplicatedList",duplicatedList);
This workaround does the trick (now I can read the bean "duplicatedList" from the session correctly) but it just doesn't seem right that the original bean loses the ArrayList on some point and still maintain the other fields.
Thanks in advance

It seems that somewhere else in your application, some code is modifying the List (since you said that it's not null but empty - if it was null I'd expect that it was removed from context at all by some other part of code). Maybe after putting List to the context some code still maintains reference and operates on it?
You can try to do the following:
request.getSession().setAttribute(Constants.SESSION_LIST_SEARCH, Collections.unmodifiableList(beanList));

Related

Java Object Clone (additional class member) using Prototype, Builder Pattern

It is not easy to explain my issue.
JPA creates some complex objects for calculations, which are stored in a database.
We decided to set the results in a working copy of this objects.
This means for each object model we created a seperated working copy model file with the same fields but some other LocalDates values and new result fields.
When the calculation was starting the working copies are instantiated.
This approach is not the best i think.
I think of the prototype pattern to clone the object.
There i come to the problem how to add the new fields. How?
Instantion costs and ist creates lots of additionals model class files.
I only think of put the result field in the calculation data models as transient fields.
Maybe inner class or local class?
I also tried to use an interface as data bucket.
But thats not the realy purpose of interfaces and also it works only with many curious trick.
For Unit Tests and user input i think it is the best to use the builder pattern and then tell JPA to store the parent object, or not?
Sorry but my answer was to long for a comment :(
There is big complex object relationship with Lists and Sets One To Many etc. relationship. When i set the result i a new class i cant determine the right object e.g. in a list. So we bild the same structurefor these result and seperated these classes in a package. Maybe it is possible to dont build the structure a second time with also references to the "basic classes". It should be sufficient to reference to each basic class a result class. It would only a little bit more navigation to get values from deeper classes. For a similiar use case there must be a best practise, or? Interfaces or sth. I very dislike the many classes for the result. Is it not possible to clone and add classmember to it for the result or to logical group easier or something like this?
It could be a solution for somebody:
http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2FIWorkingCopy.html
Here you will work with the Eclipse API and create IWorkingCopies.
For the described task toooo much.

How to create properties class handler with using default properties

phew there are lots of questions from my side these days, but:
I'm looking for the best solution of how to handle properties file when one of them are missing, than use default values. I'm using:
serenity-bdd, selenium, java, maven.
My solution is:
Create Interface with constants
Create handler which is going to have at least 3 methods like,
getDefaultProperties,
getCustomProperties
and last is method which should return list of properties, but if defaultValue != customValue than override and put to array and return, then I'm going to handle each parameter where I need.
Appreciate for advice,
Thank you.

Jackson JSON Prefixing

I'm currently using RestEasy(2.3.6) with Jackson(1.9.9) and needing to prefix my JSON arrays with '{} &&' in order to prevent JSON hijacking.
I'm new to Jackson and am having a really hard time understanding where to insert anything like this. I'm not even sure where to insert something like this to make it happen all the time, and I would like to take it one step further and be able to specify to only prefix return values that contain JSON arrays and not regular objects.
I imagine there is a class somewhere I need to subclass and override a method, and then register that class somehow. Has anyone ever done anything like this?
Jukka, the question you linked to led me to a solution. I extended JacksonJsonProvider, and overrode the writeTo() method. There are a few conditions in there and I was able to add jg.writeRaw("{}&&"); before each place it writes the value. Also, since I'm using Spring, I had to annotate my class with #Component in order for it to be found.
Also another gotcha with creating your own JsonProvider subclass is your rest methods must have #Produces('application/json') (you should always be explicit with these anyway) or else the default JsonProvider will be used.

Accessing Map elements via Enum key in Freemarker

I'm trying to access elements in a HashMap.
The keys of this HashMap are defined by an Enum.
After going through the documentation, I figured that in order to be able to access the Enum, I have to send it to Freemarker like so:
BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
TemplateHashModel enumModels = wrapper.getEnumModels();
TemplateHashModel fieldTypeModel = (TemplateHashModel)enumModels.get("com.example.MinisiteFieldType");
root.put("fieldtypes", fieldTypeModel);
In my .ftl I tried the following:
${myelement.mymap[fieldtypes.SEOTEXT]}
However I get:
Expression myelement.mymap[fieldtypes.SEOTEXT] is undefined on line X...
I wanted to make sure I mapped the enum correctly, so I tried:
${fieldtypes.SEOTEXT}
This didn't print anything, leaving me wondering wether it got through, or simply couldn't be printed.
In Java debug, just before inserting the TemplateHashModel in my root Map, 'fieldTypeModel' is filled with the correct data...
Any help would be appreciated!
Bart
I suspect your problem is that by using the [] syntax to access your map, you're implicitly telling Freemarker to treat it as a hash. That may not work as you'd expect -- the hash will probably represent the Map object, mapping 'size' and 'containsKey' and so forth to Java methods. Instead, try:
${myelement.mymap.get(fieldtypes.SEOTEXT)}
As a side note, I've never tried accessing enums by the method you describe, but there's another way to access Java constants from a template that doesn't require Java code, so the following should also work:
${myelement.mymap.get(stack.findValue("#com.example.MinisiteFieldType#SEOTEXT"))}

Accessing Request object from custom JSP tags

I'm trying to make a set of custom tags that encapsulate form elements (markup and validation).
There's a method given to retrieve the "Out" object easily:
JspWriter out = getJspContext().getOut();
However I can't figure out how to get the request object. I want to be able to directly access the submitted form values from within the Tag class so that I can validate each field.
The documentation is quite sparse, so I thought maybe I could use the JspContext object to somehow get the request attributes. But I don't understand the different scopes.
System.out.println(getJspContext().findAttribute("field1"));
always prints "null".
Enumeration e = getJspContext().getAttributeNamesInScope(1);
Looping through and printing out the enumeration just gives me a list of classes that don't exist:
javax.servlet.jsp.jspOut
javax.servlet.jsp.jspPage
javax.servlet.jsp.jspSession
javax.servlet.jsp.jspApplication
javax.servlet.jsp.jspPageContext
javax.servlet.jsp.jspConfig
javax.servlet.jsp.jspResponse
javax.servlet.jsp.jspRequest
So is this even possible?
If not, could anyone point me to a tag library that deals with form display and validation? I searched the internet for a couple hours and it seemed every single one was discontinued and I couldn't download them. Either that or suggest a better alternative for handling forms.
Edit: The tags extend the SimpleTagSupport class.
If your class is extending TagSupport, you can access the protected pageContext variable. From that you're able to retrieve the request object.
http://java.sun.com/webservices/docs/1.5/api/javax/servlet/jsp/tagext/TagSupport.html#pageContext

Categories