Jamalissimo
Jamalissimo

Reputation: 127

Load file from server to Liferay portlet

I am developing Liferay portlet. The portlet should load xml file from same server as Liferay and then via ajax parse this xml and pass these values to jQuery. I started with html file to test the functionality only. So far everything works but when I put html and js code to portlet then jQuery works but I cannot load file. I would like to have xml file located at /etc/config_files/settings.xml

Here is part of jQuery my script

function getToolContent(tool){
     var layer = '.links_' + tool.toLowerCase();
     $(layer).text("");

     $.ajax({
        type:"GET",
        url: "/etc/config_files/settings.xml",
        dataType: "xml",
        success: function(xml){
               $(xml).find("tool").each(function() {              
                   if($(this).find('section').text() == tool){
                     var title = $(this).find('title').text();
                     var text = $(this).find('text').text();

                     $(this).find("link").each(function() {
                        var label = $(this).find('label').text();
                        var referer = $(this).find('referer').text();

                        $(layer).append('<a href="' + referer + '" class="formatted_link" alt="'+ tool + ' link">'+ label +'</a><br>'); 
                     });
                     $("#text").html('<h2>'+ title + '</h2>' +'\n'+ text);

                   }
               });
           }
     });
  }

and here is sample of xml file

<?xml version="1.0" encoding="UTF-8"?>
<descriptions>
   <tool>
      <section>TOOL NAME</section>
      <title>TOOL TITLE</title>
      <text>
         <![CDATA[ 
         <ul>
            <li>FEATURE 1</li>
            <li>FEATURE 2</li>
            <li>FEATURE 3</li>
         </ul>
         ]]>
      </text>
      <links>
         <link>
            <label>TOOL LINK LABEL 1</label>
            <referer>https://mytool1.com/</referer>
         </link>
         <link>
            <label>TOOL LINK LABEL 2</label>
            <referer>https://mytool2.com/</referer>
         </link>
      </links>
   </tool>
   <tool>
      ...
   </tool>
</descriptions>

Simply I cannot get data from settings.xml

-Thanks for help

Upvotes: 0

Views: 2275

Answers (2)

Jamalissimo
Jamalissimo

Reputation: 127

in case that I have xml inside portlet then this helped:

 $.ajax({
            type:"GET",
            url: "<%= request.getContextPath()%>/js/settings.xml",
            dataType: "xml",
 ...

settings.xml is in $PORTLET-DIR/docroot/js/

I the second case when I tried to load xml anywhere from server

@Override
public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
    response.setContentType("application/xml;charset=UTF-8");
    response.getPortletOutputStream().write("http://mydomain.com/settings.xml");
}

I get this eclipse error message

Multiple markers at this line
    - The method write(int) in the type OutputStream is not applicable for the arguments 
     (String)
    - url cannot be resolved to a variable

I would like to be able to read the xml from the same server as Liferay or, if it is possible, read it from different server

Thanks

Upvotes: 1

Mark
Mark

Reputation: 18827

Solution #1: Get resource from portlet context

Put the settings.xml to $PORTLET-DIR/docroot/etc/config_files/settings.xml. Now the settings.xml is existing in the portlet cotentx, and you can get this directly from jsp. E.g. with Liferay <%=request.getServletContext()%> jsp-tag:

...
$.ajax({
       type:"GET",
       url: "<%=request.getServletContext()%>etc/config_files/settings.xml",
...

Solution #2: Get resource from anywhere by portlet ResourceURL

Create Liferay MVC-Portlet. Put to the portlet serveResource(...) methode, like:

@Override
public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
    response.setContentType("application/xml;charset=UTF-8");
    response.getPortletOutputStream().write(... your settings.xml ...);
    ...
}

Create jsp file that include your javascript and resourceURL, like:

<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
...
<liferay-portlet:resourceURL var="url" id="myid" />

...
$.ajax({
           type:"GET",
           url: "${url}",
...

Upvotes: 0

Related Questions