How do I find the URL of my web service? - java

I know this might seem a stupid question but I am just not able to find any information regarding this question.
I have a java web service (generated using NetBeans) and inside the web service class I would like to know the URL at which the web service was deployed.
For example, if I am deploying the web service on my local glassFish server, the web service is available at "http://localhost:8080/MyService/" where "MyService" is the name of my service.
The reason I need to know this URL is because my web service generates some files that I need to make available at this URL. For example, the web service call returns a URL "http://localhost:8080/MyService/report.html"
I have found some links about "WebServiceContext" but I am not able to get the URL at which my web service is running.
Edited
To clarify: Inside MyWebService.java class I want to find out the URL at which my web service was deployed (in this case, my web service is running at "http://localhost:8080/MyService/", but once it is deployed on a production server, this URL will change)

Easier in my opinion, for example:
#GET
public URI redirectSendMail(#Context UriInfo ui) {
return ui.getBaseUri();
}
If we want to send a String back to the client, indicating the exact path of some resource, we might want something like this.
#GET
public String getResourcePath(#Context UriInfo ui) {
return ui.getAbsolutePath();
}

If you are asking how to find the hostname (e.g. 'localhost' or 'www.example.com') that your servlet container is listening to you have a few options:
Add a configuration point that is set at deployment time (e.g. config file, system property)
Look into if your servlet container exposes any 'virtual host' configuration via JMX or read its config files (e.g. tomcat hosts)
Find the IP Address of the server and do a DNS lookup to get its configured hostname
Inspect the 'Host' header of the incoming HttpServletRequest
String hostname = request.getRequestHeader("Host");

Add the below property in your webservice class.
#Resource
private WebServiceContext wsCtxt;
Now below code snippet will give you the values you are looking for:
MessageContext msgCtxt = wsCtxt.getMessageContext();
HttpServletRequest request =
(HttpServletRequest)msgCtxt.get(MessageContext.SERVLET_REQUEST);
String hostName = request .getServerName();
int port = request .getServerPort();
to check from where the webservice was invoked, use the below code.
String clientIP = request .getRemoteAddr();
Related imports are below:
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import weblogic.jws.Context;
import weblogic.wsee.jws.JwsContext;

Hopefully I am able to help you, I have just recently started working with webservices (Jersey REST) and I have found that the url to your endpoint is :
'http://localhost:8080/MyService/XXX/YYY'
where XXX = the URL pattern in the servlet mapping in your web.xml file (eg. file below)
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
and the YYY is the path defined by your webservice's #Path() parameter so in the case of this example it would be something like:
'http://localhost:8080/MyService/rest/myPathDefinition'
Another thing to note is that you can in fact change the web context root in eclipse, though it will default to the name of your Java project. Please let me know if you need further clarification or if this did not help / someone can expand on it.

It could be found on your wsdl file as:
therefore: http://localhost:8080/TestSOAPWebservice/services/TestClassImpl?wsdl would be the url to your wsdl file.

Related

Where to get deployed service and generated wsdl using websphere

I am using WebSphere to publish my service as a web service using #WebService annotation in eclipse.
Details of the server :
Product name: WebSphere Application Server
Product Version: 17.0.0.2
Product edition: BASE_ILAN
While deploying the project on the server, the project started successfully and now I wanted to see the generated WSDL through admin console(as I do in Glassfish).But I am not finding any way to view the admin console or any directory which is having the generated WSDL(I am using mac os)
However, I can see(in eclipse) my service is deployed successfully, see attached image:
Thanks in advance.
As far as I know it's not available in the admin center however you can retrieve it from the service using a web browser.
Look in messages.log to find the context root of your webservices app, you'll see something like:
Web application available (default_host): http://localhost:29080/hello_jaxws/
Then to find the service you might need to know a bit about the service. If the name of the service isn't in an #WebService annotation, and the class isn't remapped in web.xml, then it's the name of the class + "Service". So in my case the name of the class is HelloService, so the url to my service is
http://localhost:29080/hello_jaxws/HelloServiceService
A browser should return
Hello! This is a CXF Web Service from that url.
Finally, add ?wsdl to it to get the wsdl
http://localhost:29080/hello_jaxws/HelloServiceService?wsdl
You can find it also when you navigate to {your service} -> Service providers -> {your service} -> WSDL document (from additional properties).
Look there for "soap:address" element, and copy the "location" value/address to your browser with anding "?wsdl" to the end of it.

Open html file on Tomcat server in Restfull web service

I hava a small web service which is running on tomcat server and i want to open my html file on my tomcat server.
My web service is:
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("myresource")
public class MyResource {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getIt() throws Exception {
String url = "E:/this.html";
File htmlFile = new File(url);
Desktop.getDesktop().browse(htmlFile.toURI());
return "Got it!";
}
}
These answers are based on the potential questions in the comments section of the question.
Q: Which url do I put in your browser to see the message "Got it!"?
A: http://localhost:8080/myresource
However, that might not be the exact case for you. There are lots of ways of setting up a web container (like tomcat) and you will have to verify what port you're running on and what your servlet context is. The above answer assumes that you are running on your local machine, under port 8080 (which tends to be the default for java web servers) and under the ROOT context.
A more complete answer would be:
http://<host_name>:<port>:<context>/myresource
Q: How do I allow the End User to say which file they want to download?
A: This is a HUGE security risk. Allowing an End User to enter a path to a file on the server is just not smart. No offense...
The End User could just enter {/etc/passwd} and depending on which system user was used to start your web container, the web container will serve that file. Not a good situation.
Q: Ok, great, so how do I allow a user to download a file from my web container?
A: There are several ways of doing this and I won't go into great detail, but, you could allow the container to serve the files themselves directly. Meaning place the files inside of the /webapp directory itself. Then your web container will serve them automajically.
You could set a directory in your MyResource classes constructor and only serve requested files from that particular directory. You would need to do some serious validation on End User input to verify that they aren't asking for a file like this: ../../../../etc/passwd
Q: FINE, got it, so I'll do validation, NOW, how do I actually serve the file to the End User? I promise I'll be careful...
Well, that is easy, just go take a peek at this question here:
what's the correct way to send a file from REST web service to client?

How to expose a JaxWS webservice endpoint at a different URL?

I have a class named AppService, annotated with JaxWS annotations: #WebService, #WebMethod.
It deploys correctly, and exposes an endpoint at the URL http://myhost/myapp/AppService.
I need to respond to a slightly different URL, at http://myhost/myapp/services/AppService (notice the 'services' segment).
How should I change the annotations or xml files in the application in order to expose the services under this new URL ?
This application is deployed on IBM WebSphere. Usage of standard JavaEE API would be preferred, but WebSphere specific instructions are fair game as well.
You need to add a servlet mapping in the web.xml: Change JAX-WS Service URL

Programmatically create subdomains with JBOSS and java

Right now I am developing an application on JBOSS 7.1 using JSF, SEAM and Primefaces. The application is providing a user registration. What I need is when the user registers an account for the nickname for example "andrew", his profile will be publicly accessed as andrew.mysite.com.
How can I implement this programmatically.
Thanks in advance,
Ilya Sidorovich
This is simply a process of mapping your subdomain to URL's that can be accessed by the appserver and use something like REST to map the URL to request parameters.
In your example, you will probably need a webserver like Apache web server to handle the incoming requests that can do some URL rewriting. Something like this
user.mysite.com --> www.mysite.com/user
In Apache this can be achieved by creating a virtualhost and using RewriteCond and RewriteRule. Here is an example
RewriteCond %{HTTP_HOST} ^([^.]+)\.mysite\.com$
RewriteRule ^/(.*)$ http://www.mysite.com/%1/$1 [L,R]
You can then forward your requests from the webserver to your application server. If using Apache this can be done using mod_jk, mod_proxy or mod_cluster.
Once you have that, you can create a RESTFul service (jboss supports REST) that can map the URL to your application code. Here is an example
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/")
public class UserService {
#GET
#Path("/{param}")
public Response printMessage(#PathParam("param") String user) {
String result = "User : " + user;
return Response.status(200).entity(result).build();
}
}

Java web service to transfer file to Local system

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!

Categories

Resources