Grails error using HttpServletResponse to export file for download - grails

I am using opencsv in my Grails application to export attributes from my Person domain class to CSV. I am receiving the following error, however:
Servlet.service() for servlet [default] in context with path [/myapp] threw exception [Request processing failed; nested exception is org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: getOutputStream() has already been called for this response] with root cause
Message: getOutputStream() has already been called for this response
From searching online, I think the answer may be to implement some responseComplete() method somewhere for the HttpServletResponse response. However, I am not sure how to do this. Any ideas? Here is my code:
def export = {
def course = Course.get(params.id)
if(course){
def persons = course ? Person.findAllByCourse(course) : []
response.setHeader("Content-disposition",
"attachment; filename=people.csv")
response.contentType = "text/csv"
def out = response.outputStream
out.withWriter { writer ->
String[] properties = new String[3]
def csvWriter = new CSVWriter(writer)
persons.each { person ->
properties[0] = person.firstName
properties[1] = person.lastName
properties[2] = person.email
properties[3] = person.phone
properties[4] = person.address1
properties[5] = person.address2
properties[6] = person.city
properties[7] = person.state
properties[8] = person.zip5
csvWriter.writeNext(properties)
}
csvWriter.flush()
}
}

Your problem probably stems from explicitly writing to the output stream in your controller, followed by the default behavior of GSP rendering upon returning from your action. You might check How to prevent Grails from rendering the default view? for another case of this, with a couple fixes. I don't have grails on the machine I'm on currently to recreate the issue, but sounds like adding an explicit return null to at the end of the closure may help. Or producing some token output or a 200 status code via render.

You have to change this proprity String[] properties = new String[3] by String[] properties = new String[9].
It works for me.

Related

Unable to bind Http Form data to HashMap collection

I am trying to post form data as
"student.id=1&courses[10].course.id=10"
The command object classes we are trying to bind are as below ...
class StudentEnrollmentCmd {
Student student
Map<String,CourseCmd> courses;
}
class CourseCmd {
CourseDomain course
}
class CourseDomain {
Long id
}
was hoping that it will bind to
"StudentEnrollmentCmd -> courses -> course.id"
which seems to work in grails 2.2.4 but fails in 3.3.7 with the following exception
No such property: course.id for class: student.CourseCmd
Here is the test case which illustrates the problem
void 'databinding from request parameters'() {
given:
// request with simple formdata: student.id=1&courses[10].course.id=10
MockHttpServletRequest request = buildMockRequestWithParams('POST',['student.id':'1','courses[10].course.id':'10']);
DataBindingSource source = bindingSourceCreator.createDataBindingSource(null,null,request);
// databinder & command object
def binder = new SimpleDataBinder()
def obj = new StudentEnrollmentCmd()
when:
binder.bind(obj,source)
then:
// this should not throw an exception, but throws an exception
MissingPropertyException ex = thrown()
System.out.println ( "Exception again:" + ex.message );
// the following should work, but does not work
obj.student.id == 1
obj.courses['10'].course.id == 10
}
Here is the link to full spec... https://github.com/swzaidi/sample/blob/master/grails3.3.7/src/test/groovy/student/DataBindingSpec.groovy
Looking for some help on how to pass the form data so that it binds to above command objects properly.

How to handle errors in my CustomAutorize attribute in asp.net 3.0 Application

I am working on an asp.net MVC 3.0 Application. I am using using my own CustomRoleProvider
and CustomErrorHandler by overriding default attributes.
Every thing is working fine. But ,the problem is with the exception handling.
While testing the application , tester has given invalid DB connection to test.
The result is , Custom Error Handler is not rendering Error View , instead it is routing the original path
For ex:
I am running my application as
Home/Index
It is first hitting Custom Role Provider to fetch the roles for the application
Since , the Db Connection is not correct , it is raising exception that "Not able to Connect"
Now , Instead of routing to Error View along with this error message. It is routing to Home Controller and Index action.
**The code for my Custom Error Handler is as Follows**
public class CustomHandleErrorAttribute : HandleErrorAttribute // Error handler
{
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
{
return;
}
if (new HttpException(null, filterContext.Exception).GetHttpCode() != 500)
{
return;
}
if (!ExceptionType.IsInstanceOfType(filterContext.Exception))
{
return;
}
// if the request is AJAX return JSON else view.
if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
filterContext.Result = AjaxError(filterContext.Exception.Message, filterContext);
}
else
{
filterContext.ExceptionHandled = true;
var controllerName = (string)filterContext.RouteData.Values["controller"];
var actionName = (string)filterContext.RouteData.Values["action"];
var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
filterContext.Result = new ViewResult
{
ViewName = View,
MasterName = Master,
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
TempData = filterContext.Controller.TempData
};
}
}
protected JsonResult AjaxError(string message, ExceptionContext filterContext)
{
if (String.IsNullOrEmpty(message))
message = "Something went wrong while processing your request. Please refresh the page and try again.";
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
return new JsonResult { Data = new { ErrorMessage = message }, ContentEncoding = System.Text.Encoding.UTF8, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
}
In the above code , after setting up filterContext.Result . It is not rendering Error View as Expected.
Please correct/suggest me, where i am going wrong..
Updated:
public class CustomRoleProvider : RoleProvider // Custom role provider
{
public override string[] GetRolesForUser(string username)
{
// Fetching roles for user from database
}
// Some other Methods
}
This is method is generating exception , since it is trying to connect to wrong connection
Updated2:
1) I am using Custom Error Handler for the entire controller.
2) I need to catch all the exceptions including Ajax Errors
3) I have included my code for Custom Error Handler Above
4) I am also using CustomRole Provider for entire controller
5) Here, I am trying to generate exception , by giving wrong database connection
6) I am running the URL : Home/Index
7) Before going to thatr URL, it is hitting the methods in Role Provider class since i am using it as a attribute
8) Since, i have gave wrong DB Connection , It is generating exception
9) Then, it fires on exception method of Custom error handler
10) Building the Error Model for the error view
11) But, here is the problem. Instead of rendering Error View , it is going to index method of the Home Controller.
12) But, i need Error View to be rendered here, because it has failed to connect to database and getting roles . I want furthuer execution of URL Home/Index to be stopped here.
Hope this clarifies the problem..i am running in to. please feel free to ask me for furthuer details/Clarification
HandleError is designed to be able to register multiple filters (for example for different exceptions). One filter can handle only some specific exceptions or error cases and another unhandle cases can be handled by another HandleError. I suppose that currently both standard and your [CustomHandleError] filter are applied. You can set the Order property to an integer value that specifies a priority from -1 (highest priority) to any positive integer value. The greater the integer value is, the lower the priority of the filter is. You can use Order parameter for example (see here) to make your filter working before. More full description of the order you can find in the MSDN documentation.
The answer, this one and the article for example provide small examples of usage Order property of HandleError.

failed to lazily initialize a collection, no session or session was closed

I know this has been asked lot of times and I have read/tried most of the solutions I can ever find. But I can not find the exact solution to my problem. Most of them are calling the save() but I am only getting the list. This is in groovy/grails.
the error "failed to lazily initialize a collection, no session or session was closed" occurs when this code is executed the return caseVisualImpairmentCauses part. When the caseVisualImpairmentCauses contains value, it returns the error "object references an unsaved transient instance - save the transient instance before flushing".
def List<CaseVisualImpairmentCause> bindVisualImpairmentCause(Long visualImpairmentPrimaryCauseId, ArrayList caseVisualImpairmentCausesList,
String visualImpairmentOtherCause) {
def caseVisualImpairmentCauses = []
if (visualImpairmentPrimaryCauseId) {
def visualImpairmentPrimaryCauseInstance = VisualImpairmentCause.get(visualImpairmentPrimaryCauseId)
CaseVisualImpairmentCause caseVisualImpairmentPrimaryCause = new CaseVisualImpairmentCause(visualImpairmentCauseIdvisualImpairmentCause: visualImpairmentPrimaryCauseInstance)
caseVisualImpairmentPrimaryCause.isPrimary = true
caseVisualImpairmentCauses << caseVisualImpairmentPrimaryCause
}
caseVisualImpairmentCausesList.each {
VisualImpairmentCause visualImpairmentCause = VisualImpairmentCause.get(it as Integer)
CaseVisualImpairmentCause caseVisualImpairmentCause = new CaseVisualImpairmentCause(visualImpairmentCauseIdvisualImpairmentCause: visualImpairmentCause)
if (it.equals('5')) {
caseVisualImpairmentCause.caseVisualImpairmentCauseOther = visualImpairmentOtherCause
}
caseVisualImpairmentCauses.add(caseVisualImpairmentCause)
}
return caseVisualImpairmentCauses
}
the one calling that is
obpCaseInstance.visualImpairmentCauses = caseService.bindVisualImpairmentCause(visualImpairmentPrimaryCauseId, listOfCaseVisualImpairmentCauses, visualImpairmentOtherCause)
any idea why this error happens?
I was able to solve this by changing all findbyId(), .get() and getbyId() into .read(). It appears that the methods(find, get) are already flushing and as good as calling .save(). I also took out all methods fetching data (methods that cant be replaced by .read() like findBYOtherproperty) being called from another service and put it in the same method instead. This made the error go away without me forcing to save the object first before leaving the service.

Writing an Integration Test in Grails that will test DB persistence

My Integration-Test for my grails application is returning a null object when I try to get a domain object using grails dynamic get method.
This is a simplified example of my problem. Lets say I have a controller TrackerLogController that uses a service TrackerLogService to save an updated Log domain for another Tracker domain.
Domain Tracker:
class Tracker {
int id
String name
static hasMany = [logs: Log]
}
Domain Log:
class Log {
int id
String comment
static belongsTo = [tracker: Tracker]
}
Controller TrackerLogController save:
def TrackerLogService
def saveTrackerLog() {
def trackerId = params.trackerId
def trackerInstance = Tracker.get(trackerId)
Log log = TrackerLogService.saveTrackerLogs(trackerInstance, params.comment)
if( log.hasErrors() ){
//render error page
}
//render good page
}
Service TrackerLogService save:
Log saveTrackerLogs( Tracker tracker, String comment) {
Log log = new Log(tracker: tracker, comment: comment)
log.save()
return log
}
So now I want to write an Integration-Test for this service but I'm not sure if I should be writing one just for the simple logic in the controller (if error, error page else good page) I would think I would write a Unit test for that, and an Integration-Test to check the persistence in the Database.
This is what I have for my Integration-Test:
class TrackerLogServiceTests {
def trackerLogService
#Before
void setUp(){
def tracker = new Tracker(id: 123, name: "First")
tracker.save()
//Now even if I call Tracker.get(123) it will return a null value...
}
#Test
void testTrackerLogService() {
Tacker trackerInstance = Tracker.get(123) //I have tried findById as well
String commit = "This is a commit"
//call the service
Log log = trackerLogService.saveTrackerLogs(trackerInstance , commit)
//want to make sure I added the log to the tracker Instance
assertEquals log , trackerInstance.logs.findByCommit(commit)
}
}
So for this example my trackerInstance would be a null object. I know the Grails magic doesn't seem to work for Unit tests without Mocking, I thought for Intigration-Tests for persistence in the DB you would be able to use that grails magic.
You can't specify the id value unless you declare that it's "assigned". As it is now it's using an auto-increment, so your 123 value isn't used. It's actually ignored by the map constructor for security reasons, so you'd need to do this:
def tracker = new Tracker(name: "First")
tracker.id = 123
but then it would get overwritten by the auto-increment lookup. Use this approach instead:
class TrackerLogServiceTests {
def trackerLogService
private trackerId
#Before
void setUp(){
def tracker = new Tracker(name: "First")
tracker.save()
trackerId = tracker.id
}
#Test
void testTrackerLogService() {
Tacker trackerInstance = Tracker.get(trackerId)
String commit = "This is a commit"
//call the service
Log log = trackerLogService.saveTrackerLogs(trackerInstance , commit)
//want to make sure I added the log to the tracker Instance
assertEquals log , trackerInstance.logs.findByCommit(commit)
}
}
Also, unrelated - don't declare the id field unless it's a nonstandard type, e.g. a String. Grails adds that for you, along with the version field. All you need is
class Tracker {
String name
static hasMany = [logs: Log]
}
and
class Log {
String comment
static belongsTo = [tracker: Tracker]
}

Sun sTax, JAXB and turning off validation against DTD/XSD/schema

We are using JAXB in conjuction with sTAX XMLEventReaderAPI to parse and extract data xml retrieved by making a REST Call.
InputStream responseStream = response.getEntityInputStream();
if (responseStream != null)
{
XMLInputFactory xmlif = XMLInputFactory.newInstance();
// stax API
XMLEventReader xmler = xmlif.createXMLEventReader(new InputStreamReader(responseStream));
EventFilter filter = new EventFilter() {
public boolean accept(XMLEvent event) {
return event.isStartElement();
}
};
XMLEventReader xmlfer = xmlif.createFilteredReader(xmler, filter);
xmlfer.nextEvent();
// use jaxb
JAXBContext ctx = JAXBContext.newInstance(Summary.class);
Unmarshaller um = ctx.createUnmarshaller();
while (xmlfer.peek() != null) {
JAXBElement<CustomObject> se = um.unmarshal(xmler,
CustomObject.class);
CustomObject = se.getValue();
}
responseStream.close();
} else {
logger.error("InputStream response from API is null. No data to process");
}
response.close();
}
So Basically we parse using sTAX first then unarshall content using JAXB which unmarshalls it the CustomObject type. We do other stuff to this CustomObject type later.
However we ran into an issue as this chunk of code executes on JBoss AS 6.1.0.Final
We get an exception saying "The declaration for the entity "HTML.version" must end with '>'"
It appears that either sTAX or JAXB is validating against a DTD/XSD. The XSD is defined on the same server to which the REST call is made.
Because we are using SUN sTAX and not woodstox that there is no inherent DTD/XSD Validation that comes with it. There is no validation and the error cannot come from the sTAX call
Is that correct ?
If the issue is not validation failure with sTAX it has got to be JAXB.
However I cannot do the following:
um.setValidating(false);
because setValidating is a deprecated method.
Any ideas/suggestions on how to go about this ? Is our hypothesis correct ? Is this a known JBoss Issue perhaps ?

Resources