I want to create a web service in java with two methods
1) to transfer a file from the internet to a local file server by returning local URL
2) to retrieve the file from the same server by taking the url
Note: It should work with all the formats
Its mandatory to use Java Web service..
any Type : Byte Array , Hexadecimal or MIME Type Transfer is OK
The size of the attachment is 4mb..
I can not connect to database directly because the application is deployed on DMZ and the only way I can connect to the file server in Intranet is by using Webservices.
Connection to the fileserver is already done..
Since you've tagged this question with soap, I'm going to assume you want a SOAP web service in Java. This also makes JAX-WS (the Java API for XML Web Services) a natural choice for the library to use. The Java(TM) Web Services Tutorial will cover your problem in greater detail.
Now you're going to need to implement the logic to take images and return URLs, and take URLs and return images.
#WebService
public class MyJavaWebService {
#WebMethod
public String takeImage(byte[] image, String imageName) {
//You'll need to write a method to save the image to the server.
//How you actually implement this method is up to you. You could
//just open a FileOutputStream and write the image to a file on your
//local server.
this.saveImage(image, imageName);
//Here is where you come up with your URL for the image.
return this.computeURLForImage(imageName);
}
#WebMethod
public byte[] getImage(String url) {
final byte[] loadedImage = this.getImage(url);
return loadedImage;
}
}
You'll also probably need to set up some additional configuration as described in Deploying Metro Endpoint. The gist of the article is that you need to add a sun-jaxws.xml file to your WEB-INF/ folder of the form
<endpoints
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="MyJavaWebService"
implementation="com.mycompany.MyJavaWebService"
url-pattern="/MyJavaWebService"/>
</endpoints>
And also add some JAX-WS stuff to your web.xml file like so:
<web-app>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>MyJavaWebServiceServlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyJavaWebServiceServlet</servlet-name>
<url-pattern>/MyJavaWebService</url-pattern>
</servlet-mapping>
</web-app>
Finally, package everything up into a .war file and deploy it to a Java web server (e.g. Tomcat).
A similar scenario:
is this and he has explained using a short code
If your main problem is to find tips for easy file transfer over a web service in java, I would recommend the Hessian service, discussed on Hessian with large binary data (java) post on SO. The link there goes for an example that implements one kind of file transfering.
Hessian is a good solution, if you don't want to mess up too much with the logic of the web services itself. Looking a Hessian code rapidly you would not even recognize you are using one. It is so light weight solution.
Stefan has a solution, where you get pretty much inside the Web Services logics, so it is up to you how high abstraction level you want to have. If the point of this task is to show how to use web services and not just get it work, then Stefan has the answer.
About file upload and such, you wanted to save a file from internet. Look this: How to download and save a file from Internet using Java?. This uses pure Java and in my understanding does not need any web services to accomplish the given task, but if you combine these two you get something that works very easily!
Related
I am trying to implement a code in our application which needs to monitor the existing registered resources in Jersey and then make decisions accordingly. I know that Jersey provides an application.wadl file which consists of all its resources at runtime.
My question is there a way to look at this file physically? Does Jersey create this file and store it somewhere in the app directory or is it in memory?
Is it possible to call any Jersey api internally on server to get the same info if this particular file is not available like that?
We are using the application class or runtimeconfig. Jersey auto discovers our resources marked with #Path annotation and we are running on Weblogic and Jersey 2.6.
Any help would be appreciated.
Thanks
No WADL file is created on disk. It is created dynamically upon URL request.
http://[host]:[port]/[context_root]/[resource]/application.wadl
E.g.:
http://localhost:8080/rest-jersey/rest/application.wadl
Also, I've found it very useful to inspect the WADL with the SoapUI tool, which can read your WADL from a URL and parse the resources into a tree format. See pic below.
I've got a Spring Web MVC application (and also a BlazeDS application, though not as relevant) where files are dynamically generated based on certain client actions.
I'd like to just map a certain directory on the file system to Spring MVC (or the app server) url and let it serve the files in that directory (with streaming and standard last-modified header support). Ideally, the mapped directory would be configured via the spring config, since I already have support per-machine for setting that up.
So, how can I do this? The best I can find so far is to write a controller that reads the file manually and streams it byte-by-byte. However, that seems far less than ideal. Is support for something like this already baked into Spring MVC or the standard application server spec?
Thanks!
If your processing model supports it, why not cut the middleman of the filesystem out of the picture completely and just stream the files back through the response stream as they are generated? Take a look at the AbstractExcelView and AbstractPDFView classes of Spring MVC to see some examples of how this is done.
or the standard application server spec?
Yes, there is. As you didn't mention which one you're using, I'll give a Tomcat-targeted answer. All you basically need to do is to add a Context element for /path/to/your/resources in /conf/server.xml:
<Context docBase="/path/to/your/resources" path="/resources" />
This way they'll be accessible through http://example.com/resources/...
Ideal for this is using an lightweight proxying server in front of your appserver, like a nginx or lighthttpd. You can configure it for serving static content, without calling your app.
If directory and files so dynamic, you can prepare real path to file at your controller and give this filepath to the frontend server, using headers. For example for nginx it's a X-Accel-Redirect header. Read more about this (and follow links for other http servers) there
I'm a newbie in web services with jax-ws and I'm a little mixed up with the different
files. I'm doing a simple hello test and I'm trying to explain something.
I successfully installed my web service on Glassfish. I also tested it with a standalone javaoutside the server) My example comes from the helloservice in the javaee5 tutorial examples. (http://java.sun.com/javaee/5/docs/tutorial/doc/docinfo.html)
In the web.xml, I have this section:
...
<servlet-mapping>
<servlet-name>HelloService</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
...
in sun-jaxws.xml
...
<endpoint
name='HelloService'
implementation='helloservice.endpoint.Hello'
url-pattern='/hello'/>
...
and in the sun-web.xml:
...
<context-root>/helloservice</context-root>
...
I see that the context root specified in the web.xml is the same as the one in sun-jaxws.xml.
The context root in sun-web.xml, does it matter or is it only used in a EAR file ?
I can access my wsdl file from 2 different addresses:
http://localhost:8080/helloservice/hello?wsdl
http://localhost:8080/helloservice/HelloService?wsdl
I can't explain the second one. Where does it come from ?
Is it a configuration of the endpoint address ?
How come we can access the web service both ways (by context-root and by Service name)?
Please explain to me
Thanks
You can configure the url to your service in many ways, in your project you have it defined in 3 different places, most of the times define something in more than one place is not a good idea.
Because you have defined 2 diffrent paths to your service, one for jax-ws and another one for the sun-web (glassfish config file), you end up with to definitions to the same service.
We want to share user validation configuration between a Java validation class (for sanity checking) and a Javascript-enabled form web interface (for usability). What's the best way to deploy this static file in our web application so that it is both available to the server-side code, and available via a URL accessed by the client?
So far I've thought of putting the file in the application root and pointing the validation class at it when it is used, and putting the file in the WEB-INF/classes directory and somehow configuring the container to serve it.
Has anyone else configured a web application like this? What did you end up doing?
Yeah. Put it in the WEB-INF/classes and have a servlet serve out the relevant portion of the validation configurations based on something like a form-id. Better yet, have the servlet transform the configuration into a JSON object and then you can simply include a script tag and start using it :)
I am new to java so excuse my lame questions:)
I am trying to build a web service in Java NetBeans 6.1 , but I have some troubles with configuration parameters ( like .settings in .net).
What is the right way to save and access such settings in a java web service.
Is there a way to read context parameters from web.xml in a web method?
If no what are the alternatives for storing your configuration variables like pathnames ?
Thank you
Is there a way to read context parameters from web.xml in a web method?
No, this is not easily done using the out-of-the-box. The Web Service system (JAX-WS) has minimal awareness of the Servlet engine (Tomcat). They are designed to be isolated.
If you wanted to use the context parameters, your web service class would need to implement ServletContextListener and retrieve the desired parameters in the initialization parameter (or save the context for later use). Since the Servlet engine and JAX-WS would each have different instances of the object, you'd need to save the values to a static member.
As Lars mentioned, the Properties API or JNDI are your best bets as they're included with Java and are fairly well-known ways to retrieve options. Use Classloader.getResource() to retrieve the Properties in a web context.
If you are using servlets, you can configure parameters in web.xml:
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
These properties will be passed in a ServletConfig object to your servlet's "init" method.
Another way is to read your system's environment variables with
System.getProperty(String name);
But this is not recommended for other than small programs and tests.
There is also the Properties API if you want to use ".properties" files.
http://java.sun.com/javase/6/docs/api/java/util/Properties.html
Finally, I believe looking up configurations with JNDI is pretty common when developing modern web service applications, Netbeans and app containers have pretty good support for that. Google it.
MessageContext ctx = MessageContext.getCurrentThreadsContext();
Servlet wsServlet = (Servlet) ctx.getProperty(HTTPConstants.MC_HTTP_SERVLET);
ServletConfig wsServletConfig = wsServlet.getServletConfig();
ServletContext wsContext = wsServletConfig.getServletContext();
I think the correct answer is ... as always ... "It depends". If you are just running a small implementation with a single server then it depend much on the WS technology you want to use. Some make the servlet context and the context-params easy to access, others don't, in which case accessing properties from a properties file may be easier. Are you going to have an array of servers in a load balanced environment with high traffic where updating the setting for all servers must be instant and centralized in-case of fail-over? If that's the case then do you really want to update the config files for all servers in the farm? How do you synchronize those changes to all those servers? Does it matter to you? If you're storing path-names in a config file then you probably intend to be able to update the path-names to another host in case certain host goes down ("\file_server_host\doc_store" --> "\backup_file_server_host\doc_store") in which case is may actually be better to fail-over using DNS instead. There are too many variables. It really depends on the design; needs; scale of the app.
For simplicity sake, if you just want a simple equivalent of a .settings file then you want a .properties file. Here is an example where I have recently used this in a project: https://github.com/sylnsr/docx4j-ws/blob/master/src/docx4j/TextSubstitution.java