RE: External access to non-AJAX resources

thumbnail
Pete Helgren, modified 7 Years ago. Regular Member Posts: 225 Join Date: 4/7/11 Recent Posts

This is a bit unusual.  I know how to create and access SB resources both within the LR ecosystem and from external sources via AJAX.  I think I have even exposed some non-SB methods using AJAX as well (can't seem to find my example though).  What I currently have is a portlet that serves up streaming media from and external source.  The DB records that point to resource are accessed through an SB method and the serveResource method in the MVCResourceCommand portlet returns the streamed media.  But I have been pondering as to how that method could be called *outside* the Liferay ecosystem.  

Any pointers on this?  I do have a page within Liferay where I can serve up (play) the streamed media using the serveResource method, but once I go *outside*, calling the method from a completely different application outside Liferay, I am at a loss as to how best to go about it.  I could move the whole thing from a portlet to a servlet but I'd like to use it as is.   
 

Thanks

thumbnail
Minhchau Dang, modified 7 Years ago. Liferay Master Posts: 598 Join Date: 10/22/07 Recent Posts
Pete Helgren:

I could move the whole thing from a portlet to a servlet but I'd like to use it as is.

What problems did you run into when you tried to access your serveResource method from outside the portal? If you ignore things like servlet filters vs. portlet filters, the only difference between a serveResource and a servlet are the extra URL parameters (which you can hide with a friendly URL route) and the CSRF token (which you can disable for specific actions or for specific portlets).

thumbnail
Pete Helgren, modified 7 Years ago. Regular Member Posts: 225 Join Date: 4/7/11 Recent Posts

Thanks...I'll try to explain this a bit better.  So I built the portlet and deployed the osgi module.  The question really revolves around invoking the serveResource method in the module without having to "expose" a portlet page, giving it a friendly URL.  You can create an SB module and deploy it and it is accessible through an AJAX call.  So I am seeking the same kind of solution: Deploy the module that contains serveResource method and then call it like I would an SB AJAX invocation.  In this case, the MVCResourceCommand portlet contains most of the code that my "old" servlet did.  I built the portlet hoping that I could serve the resources both internally to LR7 portlets as well as external clients.

The "old" servlet deployed as lecture.war had a servlet called LectureManager and a method called accessLecture and was passed a lecture number as a parameter.  So I could call that like so:

http://localhost:8080/lecture/LectureManager?action=accessLecture&lecturenumber=1234

Actually that URL is the src element in an audio tag on the HTML page that plays the lecture and it works fine...I am just trying to adapt it to the LR7 environment.  So, IF I can treat a "serveResource" like I would a servlet, what would a URL invocation look like?  Remembering that the page is not served from Liferay so I couldn't use some of the AUI syntax I see posted elsewhere.  This should be "invocable" from a web page hosted on another server.

I'll experiment a bit more but if you have an example of how to invoke the method, showing the client side code, that would be helpful....

thumbnail
Minhchau Dang, modified 7 Years ago. Liferay Master Posts: 598 Join Date: 10/22/07 Recent Posts
Pete Helgren:

The question really revolves around invoking the serveResource method in the module without having to "expose" a portlet page, giving it a friendly URL.

Ah, if the portlet won't exist on any page at all on the remote server you're trying to access from your client, that's not going to be possible. For example, Liferay's web services for remote portlets (WSRP) implementation still adds every remoteable portlet to a hidden page in order to work. Without that, you have to do a lot of the same work as the portlet container in order to prepare the request for serveResource processing.

If you want to reuse a lot of your code, you can add a servlet to OSGi, following the Servlet sample, and create a way for the two to share code. However, you won't be able to just dispatch to the serveResource method without having an actual ResourceRequest and ResourceResponse.

thumbnail
Pete Helgren, modified 7 Years ago. Regular Member Posts: 225 Join Date: 4/7/11 Recent Posts

Yeah...pretty much where I landed as well.  And I discovered that I had actually done that before:  Created a servlet.  The reason my searches for the code failed was because I had a "catastrophic loss" of my source earlier this year.  The war file was deployed and preserved but what is killing me now is HOW to create and configure one.  So maybe you can help me there....The servlet source I can get but the *environment* setup is the tricky thing....gradle..and bnd settings were lost and outside of the MANIFEST.MF file in the war, I have to guess at the settings I used to create the war file originally.

You included a link to a "Servlet sample" in your reply but it really doesn't take me anywhere. Just another categorized list.   Is there a link to some source?   My issue is: what are the steps to go through to "make a servlet"?  This is what I did so far:

1) Created a "Liferay Module Project"  but immediately got stuck on what KIND of module to create so radonly chose to use a "service" project template...maybe I should have stuck with an MVC Portlet template....unfortunately there isn't a "servlet" project template.

2) Retrieved the "lost" source from my original servlet so I could rebuild one.

3) Added what I needed to the Gradle build file so it would compile. 

4) Added Web-ContextPath: /mypath to the BND file

 

But now I am stuck....It will build a jar but not a war for deployment.  So I don't know HOW I did that before.  I looked at a few examples I found but nothing that mentions how to get gradle to build a WAR file instead of a jar....

I am very thankful for your help...I just need a good, *detailed* step by step to create a servlet so if you can point me to it or fill in the blanks I would appreciated it..

thumbnail
Pete Helgren, modified 7 Years ago. Regular Member Posts: 225 Join Date: 4/7/11 Recent Posts

I think I pretty much sorted this out.  I ended up down a rabbit trail trying to get the project to build a war file, which isn't really necessary.  Going back to my original servlet, I discovered that it was a .jar file in the first place...I just *thought* .war because, well, most servlets are...

I still need to do some additional testing, and I'll post back here, but the steps above seem to work.  Once I have it all sorted, I'll post back and maybe put up the project code in my Github account...

thumbnail
Minhchau Dang, modified 7 Years ago. Liferay Master Posts: 598 Join Date: 10/22/07 Recent Posts
Pete Helgren:

You included a link to a "Servlet sample" in your reply but it really doesn't take me anywhere. Just another categorized list.

I forgot to add an https:// at the beginning of the link, so it was treated as a relative link instead of an absolute link (which of course isn't going to work). You've gotten things working so it's not as important, but I've fixed it in case anyone else stumbles into this post in the future.