Java javax.portlet.ResourceRequest 代码实例

・21 分钟阅读

以下是展示如何使用javax.portlet.ResourceRequest的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
        response.setContentType("text/html");
        String cType = request.getParameter("context_type");
        if (cType != null) {
                PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/summary_transcriptomics.jsp");
                prd.include(request, response);
        }
        else {
                PrintWriter writer = response.getWriter();
                writer.write("<p>Invalid Parameter - missing context information</p>");
                writer.close();
        }
}
 

实例 2


public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
        response.setContentType("text/html");
        String cType = request.getParameter("context_type");
        if (cType != null) {
                PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/summary_structure.jsp");
                prd.include(request, response);
        }
        else {
                PrintWriter writer = response.getWriter();
                writer.write("<p>Invalid Parameter - missing context information</p>");
                writer.close();
        }
}
 

实例 3


@Override
public void serveResource(
                ResourceRequest resourceRequest, ResourceResponse resourceResponse)
        throws IOException, PortletException {
        try {
                String resourceID = resourceRequest.getResourceID();
                if (resourceID.equals("attachment")) {
                        serveAttachment(resourceRequest, resourceResponse);
                }
                else if (resourceID.equals("kbArticleRSS")) {
                        serveKBArticleRSS(resourceRequest, resourceResponse);
                }
        }
        catch (IOException ioe) {
                throw ioe;
        }
        catch (PortletException pe) {
                throw pe;
        }
        catch (Exception e) {
                throw new PortletException(e);
        }
}
 

实例 4


/**
 * The serveResource method is used for handling AJAX requests. It's used for the rendering of sub-menus. Anytime users
 * clicks on the menu item, the URI parameter is passed to the serveResource method. This parameter contains the URI of the
 * node which sub-menu is about to be rendered.
 *
 * @see javax.portlet.GenericPortlet#serveResource(javax.portlet.ResourceRequest, javax.portlet.ResourceResponse)
 *
 * @param request the resource request
 * @param response the resource response
 * @throws PortletException if the portlet has problems fulfilling the rendering request
 * @throws IOException if the streaming causes an I/O problem
 */
@Override
public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
    String resourceID = request.getResourceID();
    if (resourceID != null && resourceID.equals(NODE_RESOURCE_ID)) {
        String siteId = request.getParameter("siteId");
        Navigation navigation = PortalRequest.getInstance().getPortal().getNavigation(SiteId.fromString(siteId));
        String chosenNodeURI = request.getParameter("uri");
        Node chosenNode = navigation.getNode(NodePath.fromString(chosenNodeURI), Nodes.visitNodes(getNodeLevel(request)));
        boolean showEmptyCategory = getShowEmptyCategory(request);
        NodeBean chosenNodeBean = generateNodeBean(chosenNode, SiteId.fromString(siteId), showEmptyCategory);
        request.setAttribute("showEmptyCategory", showEmptyCategory);
        request.setAttribute("parentNode", chosenNodeBean);
        PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher("/jsp/node.jsp");
        prd.include(request, response);
    }
}
 

实例 5


@Override
public void serveResource(WebuiRequestContext context) throws Exception {
  super.serveResource(context);
  ResourceRequest req = context.getRequest();
  String nodeURI = req.getResourceID();
  JSONArray jsChilds = getChildrenAsJSON(nodeURI);
  if (jsChilds == null) {
    return;
  }
  MimeResponse res = context.getResponse();
  res.setContentType("text/json");
  res.getWriter().write(jsChilds.toString());
}
 

实例 6


@Override
public final void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
        String id = request.getResourceID();
        PrintWriter out = response.getWriter();
        if (id.equalsIgnoreCase("cdchatupdate")) {
                getAjaxChatUpdate(request, out);
        } else if (id.equalsIgnoreCase("cdchatpostmsg")) {
                postAjaxMessage(request, out);
        } else if (id.equalsIgnoreCase("cdchatbanuser")) {
                ajaxBanUser(request, out, true);
        } else if (id.equalsIgnoreCase("cdchatunbanuser")) {
                ajaxBanUser(request, out, false);
        } else if (id.equalsIgnoreCase("cdchatapprovemsg")) {
                ajaxApproveMsg(request, out, true);
        } else if (id.equalsIgnoreCase("cdchatrejectmsg")) {
                ajaxApproveMsg(request, out, false);
        } else if (id.equalsIgnoreCase("cdchatopenroom")) {
                ajaxOpenRoom(request, out);
        } else if (id.equalsIgnoreCase("cdchatcloseroom")) {
                ajaxCloseRoom(request, out);
        } else if (id.equalsIgnoreCase("cdchatexport")) {
                exportMessages(request, response);
        }
}
 

实例 7


@Override
public void serveResource(
                ResourceRequest resourceRequest, ResourceResponse resourceResponse)
        throws IOException, PortletException {
        try {
                String resourceID = resourceRequest.getResourceID();
                if (resourceID.equals("image")) {
                        serveImage(resourceRequest, resourceResponse);
                }
        }
        catch (IOException ioe) {
                throw ioe;
        }
        catch (PortletException pe) {
                throw pe;
        }
        catch (Exception e) {
                throw new PortletException(e);
        }
}
 

实例 8


/**
 * @see org.jboss.portletbridge.bridge.controller.BridgeController#renderResource(org.jboss.portletbridge.bridge.context.BridgeContext)
 */
public void renderResource(BridgeContext bridgeContext) throws BridgeException {
    FacesContext facesContext = null;
    Lifecycle facesLifecycle = null;
    try {
        facesLifecycle = getFacesLifecycle();
        facesContext = getFacesContext(bridgeContext, facesLifecycle);
        ResourceHandler resourceHandler = facesContext.getApplication().getResourceHandler();
        ResourceRequest resourceRequest = (ResourceRequest) bridgeContext.getPortletRequest();
        String resourceId = resourceRequest.getResourceID();
        if (resourceHandler.isResourceRequest(facesContext)) {
            // JSF2 Resource
            resourceHandler.handleResourceRequest(facesContext);
        } else if (null != resourceId) {
            if (isAutomaticResourceDispatchingEnabled(bridgeContext)) {
                renderNonFacesResource(bridgeContext, resourceId);
            }
        } else {
            renderFacesResource(bridgeContext, facesContext, facesLifecycle);
        }
    } catch (Exception e) {
        throwBridgeException(e);
    } finally {
        if (null != facesContext) {
            releaseFacesContext(bridgeContext, facesContext);
        }
    }
}
 

实例 9


@Override
public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException
{  
   String resourceID = request.getResourceID();
   if (resourceID.equals("resourceFromWriter"))
   {
      response.setContentType("image/png");
      Writer writer = response.getWriter();
      writer.write("foo");
      response.setContentType("text/html");
      writer.close();
   }
   else if (resourceID.equals("resourceFromStream"))
   {
      response.setContentType("image/png");
      OutputStream stream = response.getPortletOutputStream();
      byte[] byteArray = {0, 1, 2, 3, 4};
      stream.write(byteArray);
      response.setContentType("text/html");
      stream.close();
   }
   else if (resourceID.equals("uploaded"))
   {
      Assert.assertTrue(request.getContentType().startsWith("multipart"));
      InputStream inputStream = request.getPortletInputStream();
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
      String line = reader.readLine();
      Assert.assertTrue(line.startsWith("--"));
      line = reader.readLine();
      Assert.assertEquals("Content-type: image/png", line);
      line = reader.readLine();
      Assert.assertTrue(line.isEmpty());
      String image = reader.readLine();
      line = reader.readLine();
      Assert.assertTrue(line.startsWith("--"));
      Assert.assertTrue(line.endsWith("--"));
      response.setContentType("image/png");
      OutputStream outputStream = response.getPortletOutputStream();
      outputStream.write(image.getBytes());
      outputStream.close();
   }   
   else
   {
      throw new PortletException("No known resource with id: " + resourceID);
   }
}
 

实例 10


@Override
public String getPathInfo() {
    PortletRequest request = getRequest();
    if (request instanceof ResourceRequest) {
        ResourceRequest resourceRequest = (ResourceRequest) request;
        String resourceID = resourceRequest.getResourceID();
        if (VaadinPortlet.RESOURCE_URL_ID.equals(resourceID)) {
            String resourcePath = resourceRequest
                    .getParameter(ApplicationConstants.V_RESOURCE_PATH);
            return resourcePath;
        }
        return resourceID;
    } else {
        return null;
    }
}
 

实例 11


@Override
public void serveResource(ResourceRequest req, ResourceResponse res)
                throws IOException, PortletException {
        if (logger.isDebugEnabled())
                debug(req);
        try {
                String rid = req.getResourceID();
                PortletBag pbag = PortletBagPool.get(PortalUtil.getPortletId(req));
                String ajaxResponse = null;
                if ("add-view".equals(rid) || "update-view".equals(rid)) {
                        ajaxResponse = PortalUtil.renderPage(pbag.getServletContext(),
                                        PortalUtil.getHttpServletRequest(req),
                                        PortalUtil.getHttpServletResponse(res), editJSP, false);
                } else if ("list-view".equals(rid)) {
                        ajaxResponse = getListDataAsJson(
                                        Integer.parseInt(req.getParameter("itemPerPage")),
                                        Integer.parseInt(req.getParameter("targetPage")));
                } else if ("update-reservation".equals(rid)) {
                        updateReservation(req);
                } else if ("delete-reservation".equals(rid)) {
                        deleteReservation(req);
                } else {
                        ajaxResponse = ERR_NO_TYPE_SPECIFIED;
                }
                PrintWriter writer = res.getWriter();
                writer.write(ajaxResponse);
                writer.flush();
                writer.close();
        } catch (ServletException e) {
                throw new PortletException(e);
        }
}
 

实例 12


public void serveKBArticleRSS(
                ResourceRequest resourceRequest, ResourceResponse resourceResponse)
        throws Exception {
        if (!PortalUtil.isRSSFeedsEnabled()) {
                PortalUtil.sendRSSFeedsDisabledError(
                        resourceRequest, resourceResponse);
                return;
        }
        ThemeDisplay themeDisplay = (ThemeDisplay)resourceRequest.getAttribute(
                WebKeys.THEME_DISPLAY);
        long resourcePrimKey = ParamUtil.getLong(
                resourceRequest, "resourcePrimKey");
        int rssDelta = ParamUtil.getInteger(resourceRequest, "rssDelta");
        String rssDisplayStyle = ParamUtil.getString(
                resourceRequest, "rssDisplayStyle");
        String rssFormat = ParamUtil.getString(resourceRequest, "rssFormat");
        String rss = KBArticleServiceUtil.getKBArticleRSS(
                resourcePrimKey, WorkflowConstants.STATUS_APPROVED, rssDelta,
                rssDisplayStyle, rssFormat, themeDisplay);
        PortletResponseUtil.sendFile(
                resourceRequest, resourceResponse, null,
                rss.getBytes(StringPool.UTF8), ContentTypes.TEXT_XML_UTF8);
}
 

实例 13


protected void serveAddFileEntry(
                ResourceRequest resourceRequest, ResourceResponse resourceResponse)
        throws Exception {
        ThemeDisplay themeDisplay = (ThemeDisplay)resourceRequest.getAttribute(
                WebKeys.THEME_DISPLAY);
        long folderId = ParamUtil.getLong(resourceRequest, "folderId");
        Folder folder = DLAppServiceUtil.getFolder(folderId);
        String fileEntryTitle = ParamUtil.getString(
                resourceRequest, "fileEntryTitle");
        String content = ParamUtil.getString(resourceRequest, "content");
        byte[] bytes = content.getBytes(StringPool.UTF8);
        ServiceContext serviceContext = ServiceContextFactory.getInstance(
                resourceRequest);
        serviceContext.setAddGroupPermissions(true);
        serviceContext.setAddGuestPermissions(true);
        serviceContext.setAttribute("sourceFileName", fileEntryTitle);
        serviceContext.setScopeGroupId(folder.getGroupId());
        JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
        FileEntry fileEntry = DLAppServiceUtil.addFileEntry(
                folder.getRepositoryId(), folderId, fileEntryTitle,
                resourceRequest.getContentType(), fileEntryTitle, StringPool.BLANK,
                StringPool.BLANK, bytes, serviceContext);
        jsonObject.put("fileEntryId", fileEntry.getFileEntryId());
        String portalURL = PortalUtil.getPortalURL(themeDisplay);
        String fileEntryURL = ShindigUtil.getFileEntryURL(
                portalURL, fileEntry.getFileEntryId());
        jsonObject.put("fileEntryURL", fileEntryURL);
        writeJSON(resourceRequest, resourceResponse, jsonObject);
}
 

实例 14


public void serveImage(
        ResourceRequest resourceRequest, ResourceResponse resourceResponse)
        throws Exception {
        ThemeDisplay themeDisplay =
                (ThemeDisplay) resourceRequest.getAttribute(
                        com.liferay.portal.kernel.util.WebKeys.THEME_DISPLAY);
        long managerId = ParamUtil.getLong(resourceRequest, "managerId");
        Manager manager = ManagerLocalServiceUtil.getManager(managerId);
        InputStream in =
                DLStoreUtil.getFileAsStream(
                        themeDisplay.getCompanyId(), CompanyConstants.SYSTEM,
                        manager.getFile());
        PortletResponseUtil.sendFile(
                resourceRequest, resourceResponse, manager.getFile(), in);
}
 

实例 15


@ResourceMapping("proposalShare-autocomplete")
 public void autocompleteRecipient(ResourceRequest request, ResourceResponse response) throws PortalException, IOException {
     ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
     String queryString = request.getParameter("term");
     try {
         DynamicQuery query = DynamicQueryFactoryUtil.forClass(User.class);
         query.add(RestrictionsFactoryUtil.ilike("screenName", queryString + "%"));
query.setLimit(0, 10);
         List<User> recipients = UserLocalServiceUtil.dynamicQuery(query);
         JSONArray result = JSONFactoryUtil.createJSONArray();
         for (User user: recipients) {
             final JSONObject userObject = JSONFactoryUtil.createJSONObject();
             userObject.put("id", user.getUserId());
             userObject.put("label", user.getScreenName());
             userObject.put("value", user.getScreenName());
             result.put(userObject);
         }
         response.getPortletOutputStream().write(result.toString().getBytes());
     } catch (SystemException e) {
         e.printStackTrace();
         sendResponseJSON(false, "The request could not be processed.", response);
     }
 }
 
讨论
淘淘あ西西 profile image