Call to Cipher.getInstance makes an ftp call - java

My application makes call to Cipher.getInstance so it can decrypt passwords.
In certain environments this is creating a problem because the Cipher.getInstance method ends up trying to make an FTP call which is hanging.
Does anyone know how to avoid this FTP call or prevent it hanging?
Here is the stacktrace.
java.net.PlainSocketImpl.socketConnect(Native Method)
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:370)
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:231)
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:213)
java.net.Socket.connect(Socket.java:643)
java.net.Socket.connect(Socket.java:590)
sun.net.ftp.impl.FtpClient.doConnect(FtpClient.java:971)
sun.net.ftp.impl.FtpClient.tryConnect(FtpClient.java:931)
sun.net.ftp.impl.FtpClient.connect(FtpClient.java:1026)
sun.net.ftp.impl.FtpClient.connect(FtpClient.java:1012)
sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:307)
sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnection.java:406)
sun.net.www.protocol.jar.URLJarFile.retrieve(URLJarFile.java:227)
sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:84)
sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:128)
sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:136)
sun.net.www.protocol.jar.JarURLConnection.getJarFile(JarURLConnection.java:103)
javax.crypto.a$0.run(Unknown Source)
java.security.AccessController.doPrivileged(AccessController.java:420)
javax.crypto.a.a(Unknown Source)
javax.crypto.a.a(Unknown Source)
javax.crypto.a.a(Unknown Source)
javax.crypto.b.a(Unknown Source)
javax.crypto.d.a(Unknown Source)
javax.crypto.d.a(Unknown Source)
javax.crypto.Cipher.c(Unknown Source)
javax.crypto.Cipher.b(Unknown Source)
javax.crypto.Cipher.getInstance(Unknown Source)
javax.crypto.Cipher.getInstance(Unknown Source)

Related

"null" in Java drawImage()

I mean the drawImage() method is not working for me. People say to do like
g.drawImage(image, 0, 0, null);
But eclipse actually deny it. It said:
The method drawImage(Image, int, int, ImageObserver) in the type Graphics is not applicable for the arguments (Image, int, int, null)
This actually make my really confused, I have read some answers from others, which they always say to put it to null.
I tried to run it, and it give me this:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
The method drawImage(Image, int, int, ImageObserver) in the type Graphics is not applicable for the arguments (Image, int, int, null)
at me.danielshe.graphics.DrawComponent.paintComponent(DrawComponent.java:29)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1200(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Can anyone help me, or is that a way to put something instead of "null"? I have tried to put "this" and even implementing the ImageObserver. But neither of them works.
Thanks.
You need an object that implements the ImageObserver interface. Normally
this is would be the component associated with the graphics object.
Using null as an ImageObserver it's not wrong. Is equivalent to this:
ImageObserver obs = null;
drawImage( img, x , y , obs );
Null can be used anywhere you would use a reference (Object). The problem arises when you need to use this reference/object, you will get a runtime exception as the object would be empty. In your example as long as your image is fully loaded when you call drawImage(), you won't have a problem, therefore you should only use null on drawImage() if you know for sure that your image is fully loaded. Now I haven't had this problem with the compiler, but I don't use eclipse, just a text editor and the command line. Try compiling with the command line or use the two lines of code from above on eclipse.
"this" will only work if the class where you are using drawImage implements the ImageObserver interface or if it extends a class that implements it, like a Jcomponent for example.

vert.x getting - failed to create a child event loop

I am creating about 150 files of around 5MB sizes. Vertx file APIs gives an exception randomly after creating 10-15 files "failed to create a child event loop".
(I am using vertx3 and java8)
Below is my code snippet (After I get a callback then only I call the function again to create the next file. So, file creation is never concurrent):
Vertx.vertx().fileSystem().writeFile(filepath,
Buffer.buffer(dataList.toString()), result -> {
if (result.succeeded()) {
System.out.println("File written");
} else {
System.err.println("Oh oh ..." + result.cause());
}
lambda.callback();
});
Below is my exception stack trace:
java.lang.IllegalStateException: failed to create a child event loop
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:68)
at io.netty.channel.MultithreadEventLoopGroup.<init>(MultithreadEventLoopGroup.java:49)
at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:61)
at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:52)
at io.vertx.core.impl.VertxImpl.<init>(VertxImpl.java:132)
at io.vertx.core.impl.VertxImpl.<init>(VertxImpl.java:126)
at io.vertx.core.impl.VertxImpl.<init>(VertxImpl.java:122)
at io.vertx.core.impl.VertxFactoryImpl.vertx(VertxFactoryImpl.java:34)
at io.vertx.core.Vertx.vertx(Vertx.java:78)
at rc_datawarehouse.CsvToJsonChunkWriter.writeCsvChunkToFiles(CsvToJsonChunkWriter.java:73)
at rc_datawarehouse.CsvToJsonReadWrite.lambda$2(CsvToJsonReadWrite.java:119)
at rc_datawarehouse.CsvToJsonReadWrite$$Lambda$16/1470881859.handle(Unknown Source)
at io.vertx.core.file.impl.AsyncFileImpl.handleData(AsyncFileImpl.java:335)
at io.vertx.core.file.impl.AsyncFileImpl.lambda$doRead$285(AsyncFileImpl.java:320)
at io.vertx.core.file.impl.AsyncFileImpl$$Lambda$17/1067800899.handle(Unknown Source)
at io.vertx.core.file.impl.AsyncFileImpl$2.lambda$done$289(AsyncFileImpl.java:408)
at io.vertx.core.file.impl.AsyncFileImpl$2$$Lambda$18/1632681662.handle(Unknown Source)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$15(ContextImpl.java:314)
at io.vertx.core.impl.ContextImpl$$Lambda$5/1780132728.run(Unknown Source)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:357)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111)
at java.lang.Thread.run(Unknown Source)
Caused by: io.netty.channel.ChannelException: failed to open a new selector
at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:128)
at io.netty.channel.nio.NioEventLoop.<init>(NioEventLoop.java:120)
at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:87)
at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:64)
... 22 more
Caused by: java.io.IOException: Unable to establish loopback connection
at sun.nio.ch.PipeImpl$Initializer.run(Unknown Source)
at sun.nio.ch.PipeImpl$Initializer.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.nio.ch.PipeImpl.<init>(Unknown Source)
at sun.nio.ch.SelectorProviderImpl.openPipe(Unknown Source)
at java.nio.channels.Pipe.open(Unknown Source)
at sun.nio.ch.WindowsSelectorImpl.<init>(Unknown Source)
at sun.nio.ch.WindowsSelectorProvider.openSelector(Unknown Source)
at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:126)
... 25 more
Caused by: java.net.SocketException: No buffer space available (maximum connections reached?): connect
at sun.nio.ch.Net.connect0(Native Method)
at sun.nio.ch.Net.connect(Unknown Source)
at sun.nio.ch.Net.connect(Unknown Source)
at sun.nio.ch.SocketChannelImpl.connect(Unknown Source)
at java.nio.channels.SocketChannel.open(Unknown Source)
at sun.nio.ch.PipeImpl$Initializer$LoopbackConnector.run(Unknown Source)
... 34 more
I got my answer from the forum here (Thanks Jez)
Vertx.vertx() is the culprit. It creates a new vertx every time leading to multiple eventloops which seems to be the problem.
I cached it in a variable, rather than using Vertx.vertx() every time and it worked
cachedVertx.fileSystem().writeFile(...)

What's the difference between http://localhost:8000/ and http://127.0.0.1:8000/ for Java Applets

I have a question/problem related to Java Applet security...
I use the Applet that has to take files from server (ASP.NET) and represent the information from it. Applet take files using the code:
URL u = new URL(getCodeBase(), filename);
BufferedReader d = new BufferedReader(new InputStreamReader(u.openStream()));
This code appears in two places:
Init() method
Some another method Test() that called manually from JavaScript
So, when I try to load the page with Applet using the URL http://127.0.0.1:8000/Test.aspx, everything works fine and I can read file content from both methods. But if I change the URL on http://localhost:8000/, only the first method works properly and I can get files content and for the second one I get the next error message in JavaConsole:
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:8000 connect,resolve)
What it the difference in this case? Why 'localhost' is impossible in this case? Is there any way how to grant access to 'localhost' the same as 127.0.0.1?
here is simplest applet's example:
public class TestApplet extends Applet {
public void init()
{
System.out.println( "init...");
readDocument();
}
public void readDocument()
{
System.out.println( "read test.txt file...");
URL base = getCodeBase();
String filename = "test.txt";
try {
URL u = new URL(base, filename);
BufferedReader d = new BufferedReader(new InputStreamReader(u.openStream()));
System.out.println(d.readLine());
System.out.println("Done!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
and next code used on the client side:
<applet archive="/Content/test.jar" code="test.TestApplet.class" name="testApplet" mayscript></applet>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
var testApplet = document.testApplet;
testApplet.readDocument();
});
</script>
this code works perfectly when I try to use http://127.0.0.1:8000/Test.aspx
and doesn't work when I user http://localhost:8000/Test.aspx. I java console I see the next:
init...
read test.txt file...
some text...
Done!
read test.txt file...
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:8000 connect,resolve)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at sun.plugin2.applet.Applet2SecurityManager.checkConnect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at test.TestApplet.readDocument(TestApplet.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.plugin.javascript.JSInvoke.invoke(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source)
at sun.plugin2.liveconnect.JavaClass$MethodInfo.invoke(Unknown Source)
at sun.plugin2.liveconnect.JavaClass$MemberBundle.invoke(Unknown Source)
at sun.plugin2.liveconnect.JavaClass.invoke0(Unknown Source)
at sun.plugin2.liveconnect.JavaClass.invoke(Unknown Source)
at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$DefaultInvocationDelegate.invoke(Unknown Source)
at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo.doObjectOp(Unknown Source)
at sun.plugin2.main.client.LiveConnectSupport$PerAppletInfo$LiveConnectWorker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
P.S.: Applet is signed.
The problem is the call from JavaScript. If you are using JavaScript to call your method, the permissions of the call get down to the intersection of the JavaScript bridge's permissions (i.e. nothing) and the permissions of your own code - even if your own code is signed.
To avoid this, and use the full privileges of your applet's code, put the security-relevant parts inside a AccessController.doPrivileged(...) call. (Of course, your applet should first check that this can't do anything malicious.)
I have no idea why it works if you are using the IP address directly instead of localhost, though.
localhost is an alias for 127.0.0.1 so you may have to set/fix it in your enviroment. Under Windows you have to edit the file C:\Windows\System32\drivers\etc\hosts.
localhost is typically resolved to both ::1 and 127.0.0.1 and the OS/libc is usually set up so that IPv6 is preferred in these circumstances.
Therefore, it's likely you're allowing only 127.0.0.1 and not IPv6 connections from ::1.

Are multiple calls to java SaxParser.parse(String, Handler) Legal?

I have a java SAX parser which I would like to call multiple times on the same Handler with different XML files. I am using this in conjunction with iText to create a multi-page PDF document where some pages have one XML tag map and other pages have another. For example,
parser.parse("xmlFile1", handler);
parser.parse("xmlFile2", handler);
When I try to do this, I get a java.lang.RuntimeException thrown with the following stacktrace:
DocumentException: java.lang.RuntimeException: The document is not open.
at com.lowagie.text.pdf.PdfWriter.getDirectContent(PdfWriter.java:695)
at com.lowagie.text.pdf.PdfDocument.newPage(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.carriageReturn(Unknown Source)
at com.lowagie.text.pdf.PdfDocument.add(Unknown Source)
at com.lowagie.text.Document.add(Unknown Source)
at myClass.myCode(Unknown Source)
org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:345)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:223)
at myClass.myCode(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
Does the SaxParser.parse method implicitly call document.close()? Or is there some other problem in my code which I need to isolate and correct?
Reusing the same parser is legal (as long as it is not used concurrently)
The parser triggers an "endDocument". This seems to close the iText document here.. But this is not done by the parser - this is code from your handler.

UnknownHostException while redirecting queries to google and getting results in JSon object

Loading classifier from D:\PROJECT\classifiers\NERDemo\classifiers\ner-eng-ie.crf-3-all2008.ser.gz ... done [2.0 sec].
Original Query was riot in India.
Parsing Queries and expanding tokens from the Ontologies..
{locations=[India], events=[riot]}
Search query is null
Something went wrong...
java.net.UnknownHostException: ajax.googleapis.com
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at org.girs2.SearchHandler.makeQuery(SearchHandler.java:35)
at org.girs2.GIRS.search(GIRS.java:37)
at org.girs2.GIRS.main(GIRS.java:62)
Exception in thread "main" java.lang.NullPointerException
at org.girs2.GIRS.search(GIRS.java:44)
at org.girs2.GIRS.main(GIRS.java:62)
Looks like you probably have or had a DNS lookup failure. This tells you what happened:
java.net.UnknownHostException: ajax.googleapis.com
If you always get this exception and you can otherwise access this host by name, then I don't know what this could be, but odds are that this was a temporary DNS failure.
What should you do when you get this Exception? Well, if the address is one that you fully expect you should be able to get to -- such as this one -- then when you catch the Exception, you sleep for a while (perhaps 10 or 15 seconds, maybe even 30 or 60 seconds, depending on your application's needs) and try again.
If after several retries -- don't just retry forever! -- you still cannot reach the site, then log a reasonable complaint or otherwise complain that you cannot reach the site and have your application exit.
There's not much you can do when DNS fails except give up or wait and try again.

Categories