Caused by: java.lang.IllegalStateException: Cannot obtain Writer because OutputStream is already in use - liferay-6

I am getting the below error when trying to download file in liferay
05:03:35,867 ERROR [[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'][PortletRequestDispatcherImpl:115] org.springframework.web.util.NestedServletException: View rendering failed; nested exception is java.lang.IllegalStateException: Cannot obtain Writer because OutputStream is already in use
org.springframework.web.util.NestedServletException: View rendering failed; nested exception is java.lang.IllegalStateException: Cannot obtain Writer because OutputStream is already in use
at org.springframework.web.servlet.ViewRendererServlet.processRequest(ViewRendererServlet.java:96)
at org.springframework.web.servlet.ViewRendererServlet.doGet(ViewRendererServlet.java:67)
Caused by: java.lang.IllegalStateException: Cannot obtain Writer because OutputStream is already in use
at com.liferay.portlet.MimeResponseImpl.getWriter(MimeResponseImpl.java:90)
at com.liferay.portlet.PortletServletResponse.getWriter(PortletServletResponse.java:207)
at com.netcracker.portal.framework.spring.templates.view.SoyDataView.renderMergedOutputModel(SoyDataView.java:93)
and below is the code, i tried searching online but none of them have worked
String defaultFileName = "hello.pdf";
resourceResponse.setContentType("application/octet-stream");
resourceResponse.addProperty(GenericServiceTransport.CONTENT_DISPOSITION, "attachment; filename= hello.pdf" );
resourceResponse.flushBuffer();
try {
out = resourceResponse.getPortletOutputStream();
out.write(backendFile.getContent());
out.flush();
out.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
return;

As first item I'd try to remove the flushBuffer call. You probably get the exception in the line following it, and flushing the buffer surely will use the output stream.
If that doesn't help, please add mute code, e.g. the complete method including its signature

Related

Spark cannot catch Size exceeds Integer.MAX_VALUE error exception?

I get this Exception while making repartition operation(decreasing partition size).
Caused by: java.lang.IllegalArgumentException: Size exceeds Integer.MAX_VALUE
While trying to catch this exception somehow below try-catch block doesn't work. It didn't catch the exception.
try{
someDF.repartition(10)
.persist(StorageLevel.MEMORY_AND_DISK)
.write.mode("overwrite").format(format).save(temp_location)
}
catch {
case ex: java.lang.IllegalArgumentException => {
// Do something else
}
But if I make exception type more generic it started to catch exception.
try{
someDF.repartition(10)
.persist(StorageLevel.MEMORY_AND_DISK)
.write.mode("overwrite").format(format).save(temp_location)
}
catch {
case ex: Exception => {
// Do something else
}
So what is the reason behind it?
Does spark somehow throw an other exception internally, different than written as error message?
Note "caused by" in the exception message; it means IllegalArgumentException is the cause of the exception and you should look at the stack trace before it for the exception class and message itself. It'll look like
Exception in thread "<thread-name>" <exception-class>: <message>
See also Setting Exception cause in java which gives this example stack trace:
Exception in thread "main" java.lang.RuntimeException: Some other message
at Exceptions.main(Exceptions.java:4)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.RuntimeException: Some message
at Exceptions.main(Exceptions.java:3)
... 5 more

Catching messages thrown by classes in a lower level

There are four levels in my application: (1) My application, (2) pivot4j, (3) olap4j, and (4) Mondrian
If I run this code in Scala:
try {
renderer.render(model, new HtmlRenderCallback(sw))
}
catch {
case ex: Exception => println(ex.getMessage)
}
The application prints the error thrown by pivot4j:
org.olap4j.OlapException: mondrian gave exception while parsing query
If I don't catch the error, the stack trace is (in bold the relevant messages):
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution
exception[[PivotException: org.olap4j.OlapException: mondrian gave
exception while parsing query]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:293)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220)
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:346)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:345)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:36) Caused
by: org.pivot4j.PivotException: org.olap4j.OlapException: mondrian
gave exception while parsing query
at org.pivot4j.impl.PivotModelImpl.getCellSet(PivotModelImpl.java:598)
at org.pivot4j.ui.table.TableRenderer.render(TableRenderer.java:425)
at olap.process.RunOlap.doRunMdx(RunOlap.scala:91)
at olap.process.RunOlap$$anonfun$runMdx$1.apply(RunOlap.scala:53)
at olap.process.RunOlap$$anonfun$runMdx$1.apply(RunOlap.scala:51)
at login.Authentication$LoggedAction$$anonfun$invokeBlock$1.apply(LoggedAction.scala:47)
at login.Authentication$LoggedAction$$anonfun$invokeBlock$1.apply(LoggedAction.scala:42)
at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:253)
at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:251)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:36) Caused
by: org.olap4j.OlapException: mondrian gave exception while parsing
query
at mondrian.olap4j.MondrianOlap4jConnection$Helper.createException(MondrianOlap4jConnection.java:858)
at mondrian.olap4j.MondrianOlap4jStatement.parseQuery(MondrianOlap4jStatement.java:375)
at mondrian.olap4j.MondrianOlap4jStatement.executeOlapQuery(MondrianOlap4jStatement.java:346)
at org.pivot4j.impl.PivotModelImpl.executeMdx(PivotModelImpl.java:646)
at org.pivot4j.impl.PivotModelImpl.getCellSet(PivotModelImpl.java:596)
at org.pivot4j.ui.table.TableRenderer.render(TableRenderer.java:425)
at olap.process.RunOlap.doRunMdx(RunOlap.scala:91)
at olap.process.RunOlap$$anonfun$runMdx$1.apply(RunOlap.scala:53)
at olap.process.RunOlap$$anonfun$runMdx$1.apply(RunOlap.scala:51)
at login.Authentication$LoggedAction$$anonfun$invokeBlock$1.apply(LoggedAction.scala:47)
Caused by: mondrian.olap.MondrianException: Mondrian Error: MDX object '[Measures].[Unit Cost]' not found in cube 'Sales'
Mondrian is the one that is originating the error, and I need to get the message that is throwing:
MDX object '[Measures].[Unit Cost]' not found in cube 'Sales'
I tried:
try {
renderer.render(model, new HtmlRenderCallback(sw))
}
catch {
case ex: MondrianException => println(ex.getMessage)
}
But is not catching the error.
Use Apache Commons ExceptionUtils to get the root exception.

Logback no ExceptionInInitializerError in file only on console

I'm using logback 1.7.5 with play framework 2.2.4, during working I'm getting such error:
Exception in thread "Thread-5" java.lang.ExceptionInInitializerError
at controllers.db.SyncDBManager.createDeviceCollectionsIfNotExists(SyncDBManager.scala)
at server.impl.logic.controller.DeviceInitializer.checkAuthenticationResponse(DeviceInitializer.java:226)
at server.impl.logic.controller.DeviceInitializer.processReceivedFrames(DeviceInitializer.java:117)
at server.impl.logic.controller.DeviceController.onReceivedPackets(DeviceController.java:77)
at server.impl.logic.io.DeviceReader.run(DeviceReader.java:130)
Caused by: java.util.NoSuchElementException: None.get
at scala.None$.get(Option.scala:313)
at scala.None$.get(Option.scala:311)
at controllers.base.MongoSyncHelper$class.$init$(MongoSyncHelper.scala:16)
at controllers.db.SyncDBManager$.<init>(SyncDBManager.scala:32)
at controllers.db.SyncDBManager$.<clinit>(SyncDBManager.scala)
... 5 more
But only on console, how can I catch such exception and write it to file?
Seems like an ordinary stack trace in a console printed with the sysout. Depending on your design if you want to log an exception with the Play logger you should do it in a catch block with one of logger's methods by passing an exception as one of parameters. For example:
try {
// code
} catch {
case e: Exception => play.api.Logger.error("An error occurred", e)
}
The logger level is up to you. If you don't have a catch block and want to log an unexpected exception that occurs in any place of an application you should log it in the Global object by overriding onError method.
override def onError(request: RequestHeader, e: Throwable): Future[SimpleResult] = {
play.api.Logger.error("An error occurred", e)
super.onError(request, e)
}
Remember that onError method is called only in the production mode.

java.net.SocketException: Invalid argument

Error receiving connection: java.net.SocketException: Invalid argument
Exception in thread "Thread-698" java.lang.RuntimeException: Did not
establish input/output at
Handin2.ConnectionHandlerImpl.run(ConnectionHandlerImpl.java:124) at
java.lang.Thread.run(Thread.java:722)
I have been searching a lot on this exception. Everywhere it seems to be the conclusion that if you compile with the JVM option -Djava.net.preferIPv4Stack=true it will solve the problem. It doesn't for me though. I am running IntelliJ and trying to implement a Chord system with Sockets and Threads. This is where the above mentioned exception is caught and thrown:
if(this.remote == null || this.node == null) {
throw new RuntimeException("Connection handler has not been properly "
+ "initalialized");
}
try{
if(this.out == null) {
this.out = new ObjectOutputStream(remote.getOutputStream());
}
if(this.in == null) {
this.in = new ObjectInputStream(remote.getInputStream());
}
} catch(IOException e) {
System.out.println("Trying to make input/output for remote " + remote);
System.err.println(node.getKey()/Constants.NORMALIZE_KEY+": Error receiving connection: " + e.toString());
return false;
}
I don't know if more code snippets are needed in order to help, but I am simply lost on this exception since the solutions when searching are not helping at all.
Any hints on what triggers this error? The socket exception is caught by the given try-catch block as a special case of an IO exception
if you compile with the JVM option -Djava.net.preferIPv4Stack=true it will solve the problem.
No. If you execute with that JVM option. Nothing to do with the compiler.

simple Restful call results in InvocationTargetException

I'm using Seam with RestEasy. RestEasy provides httpClient type functionality, among other things.
An action of mine that is called when a page loads (that is, it is called from the xxx.page.xml). The code looks like:
try {
ClientRequest request = new ClientRequest(url);
ClientResponse response = null;
response = request.get();
} catch (Exception e) {
e.printStackTrace();
}
the e.printStackTrace() is never reached, but while trying the request.get(), the code generates the following exception:
Exception during request processing:
Caused by javax.el.ELException with message: "java.lang.reflect.InvocationTargetException"
org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:339)
org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:348)
org.jboss.el.parser.AstPropertySuffix.invoke(AstPropertySuffix.java:58)
org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
org.jboss.seam.core.Expressions$2.invoke(Expressions.java:175)
org.jboss.seam.navigation.Page.preRender(Page.java:311)
org.jboss.seam.navigation.Pages.preRender(Pages.java:350)
org.jboss.seam.jsf.SeamPhaseListener.preRenderPage(SeamPhaseListener.java:561)
org.jboss.seam.jsf.SeamPhaseListener.beforeRenderResponse(SeamPhaseListener.java:472)
org.jboss.seam.jsf.SeamPhaseListener.beforeServletPhase(SeamPhaseListener.java:148)
org.jboss.seam.jsf.SeamPhaseListener.beforePhase(SeamPhaseListener.java:118)
com.sun.faces.lifecycle.Phase.handleBeforePhase(Phase.java:214)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:96)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
Why would a Reflection API call even be involved????