How to call a local service in mvc portlet? - java

I have created a my-services-portlet in which i have AbcService, which I am calling from my templates like this
#set ($VeloToolsService = $serviceLocator.findService('my-services-portlet, 'com.mycompany.services.AbcServiceLocalService'))
#set ($article = $AbcService.getArticle($list))
$journalContentUtil.getContent($article.groupId, $article.articleId, 'view', $themeDisplay.language-id, $xmlRequest)
Now due to new requirement, i need to call the same service from another custom mvc portlet. I am not able to the service handle for the service. what is the proper way to get the service handle? so that i can call my existing service from the portlet.

copy the AbcService-service.jar file from first portlet to tomcat/lib/ext folder, restart liferay, and you get access to the service classes.
See http://www.liferay.com/de/community/forums/-/message_boards/message/4585610 the answer from Mika Koivisto.

Related

Should I always write extra configuration in Deployment Descriptor everytime I create a servlet in eclipse?

In eclipse, everytime new servlet is created, it will not be automatically registered in web.xml. But why is that so ? I mean, from what I have read, DD is very efficient, for example, when we ever want to change something in our web app, like changing url pattern of servlet, we don't have to find the class, the container will handle it by its own. Should i register every servlet that i create in DD ?
Yes every servlet needs to be registered and mapped. Nowadays you can also do it via the #Servlet annotation.

Servlet url parameter

I’m working on Java Servlets web application. I have a html file “searchPage.html” in the WebContent folder. I have included the “searchPage.html” name in the welcome-file list of web.xml. Now whenever I run the servlet, the searchPage.html is run. The url is
http://localhost:8080/HeadersTest/ .
“HeadersTest” is the name of the web app. Now my question is, I would like to add some parameters in the url following the “HeadersTest”.The parameters shall appear after the web app is run. Do I need to add these parameters in the service methods (doGet, doPost etc.)? For example:
http://localhost:8080/HeadersTest?paramName1=paramValue1&paramName2=paramValue2.
I’m fairly new to servlet. If someone can point me in the right direction, that will be really helpful. I have attached the screenshot of my directory structure of my web application below:
Update:
As I have listed the "searchPage.html" in welcome-file list of web.xml, "searchPage.html" launches whenever I run the web application. I would like to add few parameters in the url when the web app launches.
Adding the parameters to the URL means this is a GET request.
just handle it in your servlet's doGet() method:
request.getParameter("paramName1");
When you want to show URL Parameters you could just go with
response.sendRedirect("url with parameters");
Generally, We pass parameters in url if you want to access those parameters in Servlet/Controller side.
If you want to use these parameters in your controller which is nothing but your MainServlet class then you should pass these parameters in url. You can access these using
request.getParameter("paramName1")
in your doGet() or doPost() method.

Web application as module of a Spring MVC application

I do have a Java web application which has several servlets, I want to turn it into an optional module of a Spring mvc application. Ideally I would like to add the module as a "dependency" to make it available and add the proper link in my primary UI.
Is there a popular way to achieve this?
Many thanks in advance!
Convert all projects to maven projects, then Servlet specific projects are modules specified in the overarching MVC parent project.
That way, at build time, all required Servlet projects are automatically built by maven whenever you build the parent... And you can optionally exclude Servlets at build time.
To convert a normal java servlet project to spring mvc you would need to do the following
1) In your web.xml you would have to specify your dispatcher servlet with and the url pattern for which it would get invoked. Specify the servlet class as the dispatcher servlet class and servlet name as a custom name for this servlet
2) Create an xml file as servletname-servlet.xml
3) Convert your custom servlets to controllers by having them implement the Controller interface and specify then in the xml file created in the second step
4) Define the mapping of these controllers to the requests
5) Define a view resolver for the requests
6) Deploy
These are broadly the steps you would need to perform to convert your servlet project to Spring MVC
Depending on your servlet container(*), you could try to put the small application in its own servlet context on same container: that means that it would be a totally independant web application.
Then you install in main application a special controller mapped to a full hierarchy that would act as a forwarding relay for the secondary application. Something like :
#Autowired
String extContext;
#RequestMapping("/optapt/app1/**")
public void relay(HttpServletRequest req, HttpServletResponse resp) {
ServletContext ext = req.getServletContex().getContext(extContext);
String extUrl = req.getServletPath();
if (req.getPathInfo() != null) {
extUrl += req.getPathInfo();
}
ext.getDispatcher(extUrl).forward(req, resp);
}
That way, provided you have a global proxy that fordibs external clients to access the second application, all security can be managed by the main, and the second will only receive forwarded request from the main.
If for any reason, you have no proxy to hide the servlet container, you will still (but only) have security by obfuscation, since the direct addresses to the secondary application will never be published, provided you consistently only write addresses relative to first application in links and form actions
(*) ServletContext.getContext() may return null for security reasons on some servlet containers.

How to use the web service client generetad by eclipse

I'm using Eclipse Juno to create a Web Service Client for consuming a WSDL SOAP web service.
It generated 2 packages, one with the web service name and another with "org.tempuri.dataset_ISI_xsd".
Inside the first package there are some classes like:
1- ServiceName
2- ServiceNameProxy
3- ServiceNameService
4- ServiceNameServiceLocator
5- ServiceNameSoapBindingStub
I'd like to know wich class should I use to call the web service methods and how to obtain the values returned by the method.
Thanks in advance
Instantiate the xxxProxy class, call the setEndpoint() method passing the url of the web service host, then call the method matching the name of the web service. The return type of the method will depend on the definition of the web service.

Configure webservice URL for client from properties file with Netbeans 7 and Axis2

I'm new to webservice development. I'm using Netbeans 7.0 with the Axis2 plugin and Tomcat 7.
I created one project for the server components where I define the web methods, and then created another project for the client components. The client was created in Netbeans by selecting New -> Webservice Client.
When you select New -> Webservice Client in Netbeans, it asks you right then for a WSDL URL. So of course I gave it the WSDL URL from my local Tomcat installation. But what about when I distribute this as a real application? The users aren't going to point their clients at http://localhost:8080/axis2/services/?wsdl. I mean, when running the client from the IDE, it all works fine, but when I distribute this (it's a labor management application by the way where you clock in / out at one or more clients and time cards are written to a central DB), each client needs to be able to point at the webservice URL of whatever production server it's supposed to connect to.
I'd like to store the webservice URL in a properties file, but don't really know what all to do programmatically at the client to make the call to the URL that's loaded from the properties file.
In my client's dist folder, if I open the JAR that netbeans created with WinZip, I see a file name jax-ws-catalog.xml that has the URL in it (which is pointed at localhost). I assume this is where the URL that's used at runtime comes from.
So what's the correct way to go about this? I've searched around, but the things I've found looking on google searches tend to show webservice calls being made in a completely different way than the auto-generated code that Netbeans puts together, and I'd like some info specific to how Netbeans creates a webservice client so that I don't end up making changes just to have the IDE overwrite them.
Thanks! Sorry for the long explanation.
-Jim
I actually figured this out in a different way, and it's probably kind of specific to the way Netbeans does things. The answer Shott85 provided is a good one also, but I think this is more specific to the way Netbeans' auto-generates code.
So I have a project where all the web methods reside named SimplyLaborServer, and a project that has the webservice client named SimplyLaborClient.
In Netbeans, under the SimplyLaborClient project in the "Generated Sources (jax-ws)" node, they have a SimplyLaborServer.java file that has a class which extends Service. It has a private URL that is hard coded to my local server's URL as follows...
url = new URL("http://localhost:8080/axis2/services/SimplyLaborServer?wsdl");
And in the default constructor, it uses this URL. But it also provides a constructor as follows where I can specify the URL...
public SimplyLaborServer(URL wsdlLocation) {
super(wsdlLocation, SIMPLYLABORSERVER_QNAME);
}
So when I have an auto-generated method that looks like this in my client...
private static String testConnection() {
simplylaborclient.SimplyLaborServer service = new simplylaborclient.SimplyLaborServer();
simplylaborclient.SimplyLaborServerPortType port = service.getSimplyLaborServerHttpSoap12Endpoint();
return port.testConnection();
}
I can simply load a Properties object that has the endpoint URL and change the one line to something like below, where props is a Properties object that has endpointUrl defined with the correct URL.
simplylaborclient.SimplyLaborServer service = new simplylaborclient.SimplyLaborServer(new URL(props.getProperty("endpointUrl")));
My only concern is that these methods are kind of auto-generated when you drag and drop them from the "Web service references" node. I don't want them to be overwritten if I make additional changes server-side.
So I'm still open to feedback if this is the correct thing to do here or not.
Thanks
This has been answered before:
How to change webservice url endpoint?
NetBeans uses plain JAX-WS to generate client code, so the answer above should work for you. You just need to add some code to get the endpoint URL from a properties file.
You are using local (http://localhost:8080/axis2/services) WSDL for just creating the required classes of the web service.
After your developments completed you can host your web service application anywhere in the web or within the local network.
After you complete the developments of web service you can locally deploy it and use the service to create the clients required classes. When you creating the client you just has to create a URL object and pass your web service URL (hosted one) as below.
PropertyResourceBundle resoureceBundle = (PropertyResourceBundle) PropertyResourceBundle.getBundle(‘Property file name and path’);
URL serviceURL = resoureceBundle. getString("Hosted_URL_Name");
ServiceClass service = new ServiceClass(serviceURL);
ServicePort servicePort = new ServicePort(service);
servicePort.getItems();
Netbeans IDE will create many classes when you created the web service client automatically.
In above sample code ServiceClass is the web service main class which is you create initially by using the local URL. Name and constructor parameters will be vary as your web service but you have to pass the web service URL (newly hosted URL) as a string.
Then with the service class you can create a port object and accessing all the available web methods you need.

Categories