Serve static files with jax.ws - java

I'm building a web front end to monitor the state of a SOAP service.
Is there any way to serve static files with jax.ws? For example Endpoint.publish("/static", new SomeStaticFileHandler()) where any requests to /static just serve the corresponding static file in my folder? Inside the static file I would like to query the state and update the page with AJAX calls.
Thanks!

The correct way to serve static files is to add a custom servlet to the web.xml.
As for the hack you want to try: serve any file type, with any content-type? It will not work, I believe. Perhaps you can serve XML files if they follow a predefined schema -- JAX-WS implementation classes return objects, not strings or streams. These objects are serialized to SOAP/XML using the schema and binding. You'd need to parse the files into objects and then return to JAX-WS runtime... and you'll get a SOAP envelope over the file content anyway.
Inside the static file I would like to
query the state and update the page
with AJAX calls
This doesn't sound like a static file to me. This is a dynamic method serving XML or JSON. The simplest answer is still a servlet.
JAX-RS (RESTful Java API) is a viable alternative too.

Related

How can I generate a WSDL file from a WSDL URL?

My problem is that I created a web service client with wsimport and when creating its service object, it fails because of the HTTPS, like that:
MyService_Service service = new MyService_Service(
new URL("https://www.aaa.com/myws/MyService?WSDL"));
So, I am trying to initialize a service object from a WSDL file, but how can I create a WSDL file from that URL "https://www.aaa.com/myws/MyService?WSDL"?
Thanks a lot.
Navigate to the URL in a browser and save the file it generates. You'll need to make sure you also save any schemas imported by the wsdl.
JAX-WS needs WSDL document every time one initializes service instance. Since issues like one you described might occur, its possible to package WSDL and associated XSD schemas, so that they would be accessible no matter what.
I'd prefer using XML catalogs, since there would be no need to change WSLD document or XSD schema.
Another option would be to specify #WebService wsdlLocation property and set path to WSDL file. Though if path to XSD schemas is absolute you'll have to modify WSLD document.
If you're working with wsimport utility version that supports clientjar option, that might save you some time.
Creates the jar file of the generated artifacts along with the WSDL
metadata required for invoking the web service.

Webserver and application server communcation

We have web application,that contains both dynamic and static contents. As our client want Static content in tomcat and dynamic content in web logic . Is it possible to do this type of configuration , how we could achieve this ? what are the support connectors or support for this?
Simple: Make sure that the URLs for the static content can be statically filtered (e.g. place static images in a /image path) and configure the webserver to serve everything that matches this path from static directories, forward the rest to the appserver.
Or vice versa: Filter the dynamic content and serve the rest statically.

Serve files from a folder different of context directory in a servlet container

I got a situation that I must serve files from different folders then the one of the context my web app is running. As an example, suppose my web app is running in a servlet context on "/opt/tomcat/webapps/ROOT/" and I must serve files existent in "/opt/my_other_folder/". These folders can be changed in runtime by the client, so I can't simply add a new context pointing to these directories. I would like a solution that I wouldn't have to rewrite a web server only for that. Also, the product I work on is generic, so I can't have a solution specific to some servlet container.
Thanks!
If you're only serving files, I would consider fronting your servlet container with something like Apache HTTP Server, where you could simply use its various directives to provide a "virtual directory" pointing to an easily configured location.
Otherwise, you could write and configure a standard Java servlet that would do essentially the same thing - storing the actual path in a Java properties file that would be read by the servlet. But while this isn't a lot of work, it would be significantly more work that the above Apache HTTP Server solution. This would be very similar to several of the answers posted at Servlet for serving static content . Specifically, you could either use or extend upon Apache Tomcat's DefaultServlet. (There are some Tomcat-specific classes used in here, but they could be easily replaced with generic equivalents.) http://balusc.blogspot.com/2009/02/fileservlet-supporting-resume-and.html looks even closer to what you'd be looking for, and it is completely generic - while still having some additional, significant features.
Either of these options would be very generic, and not specific to any particular servlet container.

Axis2 web service, bottom-up approach, complex object

I am using axis2 to expose a method of existing class as a web service (bottom-up approach). The method takes a complex object (non-primitive type) as a parameter and returns a complex object as well.
I understand that axis2 will try to generate the schema for me in the wsdl file when I expose the method as a web service, and I can get the wsdl file by visiting the web service url and append ?wsdl into the end of it.
But upon closer examination, some of the attributes of the complex type in the parameters are represented as xs:anyType in the schema part of the resulting wsdl. The attributes that are converted into xs:anyType is a List. The bad thing with this is that when I generate the stub code for the client code, the method signature to set that particular attributes will take in an object as a parameter i.e. setAttribute(Object obj).
So my solution to this is to use JAXB 2.0 to generate the xml schema of the classes I need and then, import the xsd into the wsdl file that is generated by axis2 (downloaded from the web service url + ?wsdl) and use the edited wsdl instead of the one automatically generated. This solution seems to be working well for the client side. The method signature to set the attributes generated by the stub code will take in the proper type i.e. setAttribute(AnotherComplexType abcd). And by using tcpmon, I can see that the xml that is sent from the client to the server seems to be correct.
However, this approach does not work well for the server side because axis2 does not use the JAXB 2.0 annotation to convert the xml received back into the classes that the exposed method will be able to process.
My question is, is there anyway to solve my problem? The possible ways I can think of is either to modify the way axis2 process the xml after receiving it (I'm okay with processing it manually if there is indeed a way), or to make axis2 work well with JAXB 2.0 annotation? Or maybe any other idea?
Note: I'm not using the JAX-WS part of axis2
In the end I solved this myself. I generated the stub code for server side using the wsdl, modify the messageReceivers to use the generated message receiver instead, write a wrapper class that implements the generated interface, write a converter to convert the object from generated types in the parameter of the wrapper class methods going to be exposed to my internal types and expose the wrapper class instead.

Java, Spring MVC, simplest way to pull any type of file (.png, .jpg, .pdf, .doc, etc) from server and give to user for download?

I have been searching on this but the info I am finding seems overly complex for what I am trying to do or involves having to specify exactly what type of file is being downloaded and use a specific Java class for that, I'm wondering if there is a way to not have to do this and to be able to use one way of doing this regardless of the file type as I am not generating the file, just taking it from where it exists in my directory. I have a set of files in a folder locally, say:
/media/files/files
and in this directory I have a variety of different files, .jpg, .doc. .xls, .png, etc and want the simplest way that I can take a given file path, retrieve it from the directory and send it back to a JSP so that it will enable the user to download the file. The info I am finding always seems to involve de-serializing from a blob, pulling from a remote location, or something like this to where the code is rather lengthy and I figure there must be a fairly simple way to do what I am trying to do given that I have the files locally and they are in regular format (not in a DB, etc)
Any advice is appreciated
EDIT Thanks to Costi I think I am on the right path now but I tried this, below, and am still not able to access the file:
I have put this in my server.xml file in my tomcat /conf:
<Context path="/files" docBase="/media/files/" />
and then tried accessing a file at http://myip:8080/files/test.txt but it is not working, in my Spring web.xml the only Servlet-mapping I have is:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/d2/*</url-pattern>
</servlet-mapping>
Any advice is greatly appreciated
FIXED: Forgot that the server.xml in Eclipse overrides the server.xml in the tomcat server which was the issue as to why it wasn't working
If you do not want any logic wrapped around the download, you can simply make the files available as static resources, provided that the authentication/authorisation stuff is handled by the container if needed. For this to work, you simply need to map Spring DispatcherServlet so that it won't handle requests to /media/files/, one simple way to achieve this would be to have the spring servlet mapped to some extension like /.html, or have mapped to a subdirectory instead of /. This way, the container will handle requests to your /media/ stuff and it will simply serve static files.
If you need to interfere with the download somehow (for instance, if you handle auth stuff outside the container or you want to take some action prior to or after every download), then you can have a View returned from your spring controller that simply proxies the requested file to the output stream, after setting the proper headers like Content-Type, length, disposition and so on. I don't know of any such built-in view, but it's pretty easy to create one yourself and perhaps write your own ViewResolver that will return an instance of that view for file names under /media/whatever.
I hope this helps.

Categories