Author: woonsan Date: Thu Dec 10 16:30:07 2009 New Revision: 889312 URL: http://svn.apache.org/viewvc?rev=889312&view=rev Log: JS2-1087: Adding initial move actions in Page Layout Service (restful)
Added: portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PageLayoutService.java (contents, props changed) - copied, changed from r889152, portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PageManagementService.java Removed: portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PageManagementService.java Modified: portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/jetspeed-restful-services.xml portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/pipelines.xml Copied: portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PageLayoutService.java (from r889152, portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PageManagementService.java) URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PageLayoutService.java?p2=portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PageLayoutService.java&p1=portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PageManagementService.java&r1=889152&r2=889312&rev=889312&view=diff ============================================================================== --- portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PageManagementService.java (original) +++ portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PageLayoutService.java Thu Dec 10 16:30:07 2009 @@ -22,42 +22,43 @@ import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.DELETE; -import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.POST; +import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; -import javax.ws.rs.core.PathSegment; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.math.NumberUtils; import org.apache.jetspeed.JetspeedActions; import org.apache.jetspeed.components.portletregistry.PortletRegistry; -import org.apache.jetspeed.decoration.DecorationValve; import org.apache.jetspeed.layout.PageLayoutComponent; import org.apache.jetspeed.layout.PortletActionSecurityBehavior; +import org.apache.jetspeed.om.page.ContentFragment; import org.apache.jetspeed.om.page.ContentPage; -import org.apache.jetspeed.page.PageManager; +import org.apache.jetspeed.om.portlet.InitParam; +import org.apache.jetspeed.om.portlet.PortletDefinition; import org.apache.jetspeed.request.RequestContext; +import org.apache.jetspeed.services.beans.ContentFragmentBean; import org.apache.jetspeed.services.beans.ContentPageBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * PageManagementService + * PageLayoutService * * @version $Id$ */ - -...@path("/") -public class PageManagementService +...@path("/pagelayout/") +public class PageLayoutService { - private static Logger log = LoggerFactory.getLogger(PageManagementService.class); + private static Logger log = LoggerFactory.getLogger(PageLayoutService.class); @Context private ServletConfig servletConfig; @@ -65,47 +66,37 @@ @Context private ServletContext servletContext; - private PageManager pageManager; - - private PortletActionSecurityBehavior securityBehavior; + private PageLayoutComponent pageLayoutComponent; private PortletRegistry portletRegistry; - private DecorationValve decorationValve; - - private PageLayoutComponent pageLayoutComponent; + private PortletActionSecurityBehavior securityBehavior; - public PageManagementService(PageManager pageManager, - PortletActionSecurityBehavior securityBehavior, - PortletRegistry portletRegistry, - DecorationValve decorationValve, - PageLayoutComponent pageLayoutComponent) + public PageLayoutService(PageLayoutComponent pageLayoutComponent, + PortletRegistry portletRegistry, + PortletActionSecurityBehavior securityBehavior) { - this.pageManager = pageManager; - this.securityBehavior = securityBehavior; - this.portletRegistry = portletRegistry; - this.decorationValve = decorationValve; this.pageLayoutComponent = pageLayoutComponent; + this.portletRegistry = portletRegistry; + this.securityBehavior = securityBehavior; } @GET - @Path("/{path:.*}") + @Path("/page/") public ContentPageBean getContentPage(@Context HttpServletRequest servletRequest, - @Context UriInfo uriInfo, - @PathParam("path") List<PathSegment> pathSegments) + @Context UriInfo uriInfo) { RequestContext requestContext = (RequestContext) servletRequest.getAttribute(RequestContext.REQUEST_PORTALENV); - ContentPage contentPage = getContentPage(requestContext); + ContentPage contentPage = getContentPage(requestContext, JetspeedActions.VIEW); return new ContentPageBean(contentPage); } @POST - @Path("/{path:.*}") - public Response addContentFragment(@Context HttpServletRequest servletRequest, - @Context UriInfo uriInfo, - @PathParam("path") List<PathSegment> pathSegments, - @FormParam("type") String fragmentType, - @FormParam("name") String fragmentName) + @Path("/fragment/{type}/{name}/") + public ContentFragmentBean addContentFragment(@Context HttpServletRequest servletRequest, + @Context UriInfo uriInfo, + @PathParam("type") String fragmentType, + @PathParam("name") String fragmentName) { if (StringUtils.isBlank(fragmentType) || StringUtils.isBlank(fragmentName)) { @@ -113,19 +104,24 @@ } RequestContext requestContext = (RequestContext) servletRequest.getAttribute(RequestContext.REQUEST_PORTALENV); - ContentPage contentPage = getContentPage(requestContext); - - pageLayoutComponent.addPortlet(contentPage, fragmentType, fragmentName); + ContentPage contentPage = getContentPage(requestContext, JetspeedActions.EDIT); - return Response.ok().build(); + try + { + ContentFragment contentFragment = pageLayoutComponent.addPortlet(contentPage, fragmentType, fragmentName); + return new ContentFragmentBean(contentFragment); + } + catch (Exception e) + { + throw new WebApplicationException(e); + } } @DELETE - @Path("/{path:.*}") + @Path("/fragment/{id}/") public Response deleteContentFragment(@Context HttpServletRequest servletRequest, - @Context UriInfo uriInfo, - @PathParam("path") List<PathSegment> pathSegments, - @QueryParam("id") String fragmentId) + @Context UriInfo uriInfo, + @PathParam("id") String fragmentId) { if (StringUtils.isBlank(fragmentId)) { @@ -133,20 +129,162 @@ } RequestContext requestContext = (RequestContext) servletRequest.getAttribute(RequestContext.REQUEST_PORTALENV); - ContentPage contentPage = getContentPage(requestContext); + ContentPage contentPage = getContentPage(requestContext, JetspeedActions.EDIT); - pageLayoutComponent.removeFragment(contentPage, fragmentId); + try + { + pageLayoutComponent.removeFragment(contentPage, fragmentId); + } + catch (Exception e) + { + throw new WebApplicationException(e); + } return Response.ok().build(); } - private ContentPage getContentPage(RequestContext requestContext) throws WebApplicationException + @PUT + @Path("/fragment/{id}/pos/") + public ContentFragmentBean moveContentFragment(@Context HttpServletRequest servletRequest, + @Context UriInfo uriInfo, + @PathParam("id") String fragmentId, + @QueryParam("layout") String layoutFragmentId, + @QueryParam("dir") String direction, + @QueryParam("row") String rowParam, + @QueryParam("col") String colParam, + @QueryParam("x") String posXParam, + @QueryParam("y") String posYParam, + @QueryParam("z") String posZParam, + @QueryParam("w") String posWidthParam, + @QueryParam("h") String posHeightParam) + { + if (StringUtils.isBlank(fragmentId)) + { + throw new WebApplicationException(new IllegalArgumentException("Fragment id not specified")); + } + + RequestContext requestContext = (RequestContext) servletRequest.getAttribute(RequestContext.REQUEST_PORTALENV); + ContentPage contentPage = getContentPage(requestContext, JetspeedActions.EDIT); + ContentFragment contentFragment = contentPage.getFragmentById(fragmentId); + + if (contentFragment == null) + { + throw new WebApplicationException(new IllegalArgumentException("Fragment not found with the specified id: " + fragmentId)); + } + + ContentFragment layoutFragment = null; + + if (!StringUtils.isBlank(layoutFragmentId)) + { + layoutFragment = contentPage.getFragmentByFragmentId(layoutFragmentId); + + if (layoutFragment == null) + { + throw new WebApplicationException(new IllegalArgumentException("Layout fragment not found with the specified id: " + layoutFragmentId)); + } + } + else + { + layoutFragment = getParentFragment(pageLayoutComponent.getUnlockedRootFragment(contentPage), fragmentId); + + if (layoutFragment == null) + { + throw new WebApplicationException(new IllegalArgumentException("Layout fragment not found for the fragment: " + fragmentId)); + } + } + + int layoutColumnCount = getColumnCountOfLayoutFragment(layoutFragment); + + if (!StringUtils.isBlank(direction)) + { + int row = contentFragment.getLayoutRow(); + int col = contentFragment.getLayoutColumn(); + + direction = direction.trim(); + + if ("left".equals(direction)) + { + if (col > 0) + { + --col; + } + } + else if ("right".equals(direction)) + { + if (col < layoutColumnCount - 1) + { + ++col; + } + } + else if ("up".equals(direction)) + { + // TODO: retrieve all portlet fragments to calculate the real row number? + if (row > 0) + { + --row; + } + } + else if ("down".equals(direction)) + { + // TODO: retrieve all portlet fragments to calculate the real row number? + ++row; + } + else + { + throw new WebApplicationException(new IllegalArgumentException("Invalid direction: " + direction)); + } + + rowParam = Integer.toString(row); + colParam = Integer.toString(col); + } + + if (!StringUtils.isBlank(rowParam) && !StringUtils.isBlank(colParam)) + { + int row = NumberUtils.toInt(rowParam, -1); + int col = NumberUtils.toInt(colParam, -1); + + try + { + pageLayoutComponent.updateRowColumn(contentFragment, row, col); + return new ContentFragmentBean(contentPage.getFragmentById(fragmentId)); + } + catch (Exception e) + { + throw new WebApplicationException(e); + } + } + else + { + float posX = NumberUtils.toFloat(posXParam, -1.0f); + float posY = NumberUtils.toFloat(posYParam, -1.0f); + float posZ = NumberUtils.toFloat(posZParam, -1.0f); + float posWidth = NumberUtils.toFloat(posWidthParam, -1.0f); + float posHeight = NumberUtils.toFloat(posHeightParam, -1.0f); + + try + { + pageLayoutComponent.updatePosition(contentFragment, posX, posY, posZ, posWidth, posHeight); + return new ContentFragmentBean(contentPage.getFragmentById(fragmentId)); + } + catch (Exception e) + { + throw new WebApplicationException(e); + } + } + } + + private ContentPage getContentPage(RequestContext requestContext, String action) throws WebApplicationException { try { - checkPageAccess(requestContext, JetspeedActions.VIEW); - // Run the Decoration valve to get actions - decorationValve.invoke(requestContext, null); + if (securityBehavior != null) + { + if (!securityBehavior.checkAccess(requestContext, action)) + { + throw new SecurityException("Insufficient access to view page"); + } + } + return requestContext.getPage(); } catch (Exception e) @@ -155,15 +293,59 @@ } } - private void checkPageAccess(RequestContext requestContext, String action) throws SecurityException + private ContentFragment getParentFragment(ContentFragment contentFragment, String fragmentId) { - if (securityBehavior != null) + for (ContentFragment child : (List<ContentFragment>) contentFragment.getFragments()) { - if (!securityBehavior.checkAccess(requestContext, action)) + if (fragmentId.equals(child.getId())) { - throw new SecurityException("Insufficient access to view page"); + return contentFragment; + } + else if (ContentFragment.LAYOUT.equals(child.getType())) + { + ContentFragment parent = getParentFragment(child, fragmentId); + + if (parent != null) + { + return parent; + } } } + + return null; + } + + private int getColumnCountOfLayoutFragment(ContentFragment layoutFragment) + { + int columnCount = 0; + + String sizes = layoutFragment.getLayoutSizes(); + + if (StringUtils.isBlank(sizes)) + { + PortletDefinition layoutPortletDef = portletRegistry.getPortletDefinitionByUniqueName(layoutFragment.getName(), true); + InitParam initParam = layoutPortletDef.getInitParam("sizes"); + + if (initParam != null) + { + sizes = initParam.getParamValue(); + } + else + { + initParam = layoutPortletDef.getInitParam("columns"); + + if (initParam != null) + { + return Integer.parseInt(initParam.getParamValue()); + } + } + } + + if (!StringUtils.isBlank(sizes)) + { + columnCount = StringUtils.splitPreserveAllTokens(sizes, ",").length; + } + + return columnCount; } - } Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PageLayoutService.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PageLayoutService.java ------------------------------------------------------------------------------ svn:keywords = Id Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/services/rest/PageLayoutService.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/jetspeed-restful-services.xml URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/jetspeed-restful-services.xml?rev=889312&r1=889311&r2=889312&view=diff ============================================================================== --- portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/jetspeed-restful-services.xml (original) +++ portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/jetspeed-restful-services.xml Thu Dec 10 16:30:07 2009 @@ -126,7 +126,7 @@ </bean> <bean class="org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider"> <meta key="j2:cat" value="default" /> - <constructor-arg ref="jaxrsPageManagementService" /> + <constructor-arg ref="jaxrsPageLayoutService" /> </bean> </list> </property> @@ -141,13 +141,11 @@ </bean> <!-- Portal Page Management JAX-RS Service --> - <bean id="jaxrsPageManagementService" class="org.apache.jetspeed.services.rest.PageManagementService"> + <bean id="jaxrsPageLayoutService" class="org.apache.jetspeed.services.rest.PageLayoutService"> <meta key="j2:cat" value="default" /> - <constructor-arg ref="org.apache.jetspeed.page.PageManager" /> - <constructor-arg ref="PortletActionSecurityBehavior" /> - <constructor-arg ref="org.apache.jetspeed.components.portletregistry.PortletRegistry" /> - <constructor-arg ref="DecorationValve" /> <constructor-arg ref="org.apache.jetspeed.layout.PageLayoutComponent" /> + <constructor-arg ref="org.apache.jetspeed.components.portletregistry.PortletRegistry" /> + <constructor-arg ref="PortletActionSecurityBehavior" /> </bean> </beans> Modified: portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/pipelines.xml URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/pipelines.xml?rev=889312&r1=889311&r2=889312&view=diff ============================================================================== --- portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/pipelines.xml (original) +++ portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/pipelines.xml Thu Dec 10 16:30:07 2009 @@ -747,6 +747,7 @@ <ref bean="profilerValve" /> <ref bean="refreshUserHomepageValve" /> <ref bean="containerValve" /> + <ref bean="DecorationValve" /> <ref bean="RestfulServicesValve" /> </list> </constructor-arg> --------------------------------------------------------------------- To unsubscribe, e-mail: jetspeed-dev-unsubscr...@portals.apache.org For additional commands, e-mail: jetspeed-dev-h...@portals.apache.org