Jetty server allow all cross origins - java

I use jetty as java lib to run my local server, that has static content. How can I configure this server to allow all cross origins?
This is the code of my server:
Server server = new Server(8087);
Context staticContext = new Context(server, "/", 0);
staticContext.setHandler(new ResourceHandler());
staticContext.setResourceBase("./static");
staticContext.setContextPath("/");
try {
server.start();
} catch (Exception e1) {
e1.printStackTrace();
}

Related

Send files to server via FTPS protocol

I'm creating an app which generates a CSV file and some PDFs. I want my app to send those files to a server via FTPS protocol.
I'm using Apache Commons Net FTP library and it was perfectly working when I had "Require TLS session resumption on data connection when using PORT P" unchecked, but since I enabled it I can't send my files.
An error appeared :
450 TLS session of data connection has not resumed or the session does not match the control connection.
After some researches on this site I have overriden _prepareDataSocket_ in order to overcome this problem but now it just creates empty files on the server.
There is my overriden function :
#Override
protected void _prepareDataSocket_(final Socket socket) throws IOException {
if (socket instanceof SSLSocket) {
// Control socket is SSL
final SSLSession session = ((SSLSocket) _socket_).getSession();
if (session.isValid()) {
final SSLSessionContext context = session.getSessionContext();
try {
final Field sessionHostPortCache = context.getClass().getDeclaredField("sessionHostPortCache");
sessionHostPortCache.setAccessible(true);
final Object cache = sessionHostPortCache.get(context);
final Method method = cache.getClass().getDeclaredMethod("put", Object.class, Object.class);
method.setAccessible(true);
method.invoke(cache, String
.format("%s:%s", socket.getInetAddress().getHostName(), String.valueOf(socket.getPort()))
.toLowerCase(Locale.ROOT), session);
method.invoke(cache, String
.format("%s:%s", socket.getInetAddress().getHostAddress(), String.valueOf(socket.getPort()))
.toLowerCase(Locale.ROOT), session);
} catch (NoSuchFieldException e) {
throw new IOException(e);
} catch (Exception e) {
throw new IOException(e);
}
} else {
throw new IOException("Invalid SSL Session");
}
}
}
and this is what FileZilla Server displays:
FileZilla Response
will this answer on another forum help?
http://forum.rebex.net/5673/450-error-connecting-to-ftp-requiring-explicit-ftp-over-tls

Jetty Server blocks when setting handler

I am trying to setup a Jetty Server with Servlet Contexts and it blocks when calling server.setHandler(context). If I run the code snippet from below, which is actually following the example from http://www.eclipse.org/jetty/documentation/9.3.x/embedding-jetty.html#_embedding_servletcontexts , it only prints one "a" and then blocks.
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
Server server = new Server(8070);
log.info("a");
server.setHandler(context);
log.info("a");
context.addServlet(EventServer.class, "/response");
context.addServlet(NotificationServer.class, "/notification");
log.info("a");
context.addEventListener(new ConfigureService());
context.addEventListener(new NotificationService());
try {
server.start();
log.info("Jetty-Server Started");
server.join();
} catch (Exception e) {
log.error(ExceptionUtils.getFullStackTrace(e));
} finally {
server.destroy();
}
Any idea why it does not execute the rest of the code and blocks when setting the handler?
It turns out that you can't actually run the jetty-server and jetty-servlet while them being different versions and this will cause the block when setting the handler.

java NCache - GeneralFailureException: No server is available to process the request

I use NCache java API for connecting to NCache server but it throws an exception:
com.alachisoft.ncache.runtime.exceptions.GeneralFailureException: No server is available to process the request.
I also tried some suggestion from this site
but cannot solve the problem.
Here are the java codes.
String cacheId = "mycache";
try {
cache = NCache.initializeCache(cacheId);
} catch (Exception ex) {
ex.printStackTrace();
log.log(Level.SEVERE, "NCacheConnection()", ex.getMessage());
}

Not able to enable JMX for Grizzly

I tried enabling the JMX for grizzly server. I added the gmbal-api-only-3.1.0 jar file to the project and wrote the following simple code :
public class Main {
public static void main(String[] args) {
HttpServer gws = new HttpServer();
NetworkListener listener1 = new NetworkListener("listener1", "localhost", 19080);
gws.addListener(listener1);
try {
gws.start();
gws.getServerConfiguration().setJmxEnabled(true);
System.in.read();
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(1);
} finally {
try {
gws.stop();
} catch (Exception ignored) {}
}
}
}
I ran this program and opened it up in the JConsole, but the JConsole do not show any MBean for the grizzly server in spite of using setJMXEnabled(true).
Please tell me what could be missing here or what is wrong with the code? Else, please suggest how to enable JMX for grizzly and how to verify it. I tried using the "Grizzly HTTP JMX Server Monitoring" approach given on the link : https://grizzly.java.net/monitoring.html

Registering with a URL for asynchronous notifications?

I am trying to understand more about async notifications. I have a URL in the form of:
http://www.sample.com/AsyncNotify?sessionId=xxxxxx
Now if I call this URL with the sessionId, it is equivalent to registering for Asynchronous notifications. I am using Apache HTTP Commons library to do Http Post and Get. If that's the case, then how can I receive events from the server side? Do I have to forget this approach and use sockets instead? Currently, this is my approach:
HttpClient httpClient = new HttpClient;
String url = "http://www.sample.com/AsyncNotify?sessionId=xxxxxx"
GetMethod get = new GetMethod(url);
try {
httpClient.executeMethod(get);
//read the response
} catch(Exception e) {
}
What I was thinking was to establish a socket level connection inside a while loop and call a handler whenever it receives some data, but is there a better way to achieve this?
EDIT:
I've used xSocket to get to the following stage but the connection closes after 30 seconds:
try {
String _GETRequest = "/sample/notify";
HttpClientConnection con = new HttpClientConnection("10.0.0.23", 5050);
con.setConnectionTimeoutMillis(100000);
GetRequest request = new GetRequest(_GETRequest);
request.setParameter("id", id);
IHttpResponseHandler responseHandler = new AsyncHandler();
con.send(request, responseHandler);
org.xlightweb.client.HttpClient httpClient = new org.xlightweb.client.HttpClient();
request.setParameter("id", id);
con.send(request, responseHandler);
// Don't let the program terminate. In other words,
// wait for a message from the server
while(con.isOpen()) {};
if(!con.isOpen()) {
}
} catch (ConnectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Tomcat came out with a technology called Tomcat Comet ( http://tomcat.apache.org/tomcat-6.0-doc/aio.html ). It has also been used for the new Servlet 3.0 spec. This technology will allow you to do persistent HTTP connections through which you can push notifications to any clients.
There is also a technolgy called WebSockets that is part of HTML 5
( http://dev.w3.org/html5/websockets/ ) Of course it only works in a limied set of browsers for now. Probably should wait on this.
Of course the current way to do it to be technolgy backwards compatible (even if it sucks) is to poll the server periodically and get results that way.
Of course if everybody (clients and servers) are on a local network then probably something like RMI or even EJBs or JMS Pub/Sub would be best.
Here is a Comet tutorial http://www.ibm.com/developerworks/web/library/wa-cometjava/index.html and another one http://www.javaworld.com/javaworld/jw-03-2008/jw-03-asynchhttp-test.html

Categories