title: Is it possible in liferay to make an ajax call independent of a portlet?
body: <p>I would like to make an ajax call from javascript code (to check if the user session is still active). I would like my code to be independent of any portlet, and hence would like to build the url in javascript, without having to rely on a portlet id.</p>
  
  <p>Is this possible within the MVCPortlet framework ? If not, are there alternatives ?</p>
  
  <p><strong>Edit below this line.</strong></p>
  
  <p>I am aware of liferay's mechanisms for session management, and I actually have set an automatic extension of the session. But two of my users have lost thier long input because the session expired despite this mechanism (I believe they interrupted their work, and their computer went to sleep...).</p>
  
  <p>I would like to avoid such a situation by checking if a session is still active before I submit an input from certain forms. The way I do it is prepend the submit with an ajax call which I detail below.</p>
  
  <p>My true question is : <strong>how do I remove the reliance on portletId in that code so that I don't need to change every portlet in which I want to implement this mechanism.</strong></p>
  
  <p>I do it today with the following javascript function:</p>
  
  <pre><code>Liferay.provide(window, "bb_checkSession",
      function (sessionId, portletId, successCallback, failureCallback) {
          var A = AUI();
          var resourceUrl = Liferay.PortletURL.createResourceURL();
          resourceUrl.setResourceId("CHECK_SESSION");
          resourceUrl.setPortletId(portletId);
          A.io.request(resourceUrl.toString(), {
              method: 'GET',
              cache: false,
              on: {
                  success: function () {
                      var responseData = A.JSON.parse(this.get('responseData'));
                      if (responseData == null || sessionId != responseData.sessionId) {
                          if (failureCallback) failureCallback();
                      } else {
                          if (successCallback) successCallback();
                      }
                  },
                  failure: function () {
                      if (failureCallback) failureCallback();
                  }
              }
          });
      },
      ['aui-io', 'liferay-portlet-url']
  );
  </code></pre>
  
  <p>and in my portlet, I handle the call as follows</p>
  
  <pre><code>public void checkSession(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException {
  
      PortletSession session = resourceRequest.getPortletSession();
  
      JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
      resourceResponse.setContentType("text/javascript");
      jsonObject.put("sessionId", session.getId());
      resourceResponse.getWriter().write(jsonObject.toString());
  }
  </code></pre>