public String addParameter(String url, String name, String value) { if (url == null) { return null; } String[] urlArray = PortalUtil.stripURLAnchor(url, StringPool.POUND); url = urlArray[0]; String anchor = urlArray[1]; StringBundler sb = new StringBundler(7); sb.append(url); if (url.indexOf(CharPool.QUESTION) == -1) { sb.append(StringPool.QUESTION); } else if (!url.endsWith(StringPool.QUESTION) && !url.endsWith(StringPool.AMPERSAND)) { sb.append(StringPool.AMPERSAND); } sb.append(name); sb.append(StringPool.EQUAL); sb.append(encodeURL(value)); sb.append(anchor); return sb.toString(); }
public static String getPortalURL(HttpServletRequest httpServletRequest) { String portalURL = null; HttpSession session = httpServletRequest.getSession(); Boolean httpsInitial = (Boolean) session.getAttribute(WebKeys.HTTPS_INITIAL); if (PropsValuesCompat.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS && !PropsValuesCompat.SESSION_ENABLE_PHISHING_PROTECTION && (httpsInitial != null) && !httpsInitial.booleanValue()) { portalURL = PortalUtil.getPortalURL(httpServletRequest, false); } else { portalURL = PortalUtil.getPortalURL(httpServletRequest); } return portalURL; }
public String removeParameter(String url, String name) { int pos = url.indexOf(CharPool.QUESTION); if (pos == -1) { return url; } String[] array = PortalUtil.stripURLAnchor(url, StringPool.POUND); url = array[0]; String anchor = array[1]; StringBundler sb = new StringBundler(); sb.append(url.substring(0, pos + 1)); String[] parameters = StringUtil.split(url.substring(pos + 1, url.length()), CharPool.AMPERSAND); for (String parameter : parameters) { if (parameter.length() > 0) { String[] kvp = StringUtil.split(parameter, CharPool.EQUAL); String key = kvp[0]; String value = StringPool.BLANK; if (kvp.length > 1) { value = kvp[1]; } if (!key.equals(name)) { sb.append(key); sb.append(StringPool.EQUAL); sb.append(value); sb.append(StringPool.AMPERSAND); } } } url = StringUtil.replace( sb.toString(), StringPool.AMPERSAND + StringPool.AMPERSAND, StringPool.AMPERSAND); if (url.endsWith(StringPool.AMPERSAND)) { url = url.substring(0, url.length() - 1); } if (url.endsWith(StringPool.QUESTION)) { url = url.substring(0, url.length() - 1); } return url + anchor; }
/** * Returns a HMTL representation of an option * * @param option option to HTML-ify * @return the HTML string */ public static String toHtml(SelectOption option) { StringBuffer html = new StringBuffer(); html.append("<option value=\"" + option.getValue() + "\""); if (option.selected) html.append(" selected"); html.append(">" + PortalUtil.htmlEscape(option.getDescription()) + "</option>"); return html.toString(); }
protected void testCanonicalURL( String virtualHostname, String portalDomain, Group group, Layout layout, Locale[] groupAvailableLocales, Locale groupDefaultLocale, String i18nPath, String expectedLayoutFriendlyURL, boolean forceLayoutFriendlyURL) throws Exception { if (!group.isGuest()) { group = GroupTestUtil.updateDisplaySettings( group.getGroupId(), groupAvailableLocales, groupDefaultLocale); } String completeURL = generateURL(portalDomain, i18nPath, group.getFriendlyURL(), layout.getFriendlyURL()); setVirtualHost(layout.getCompanyId(), virtualHostname); ThemeDisplay themeDisplay = getThemeDisplay(group); themeDisplay.setPortalURL("http://" + portalDomain + ":8080/"); String actualCanonicalURL = PortalUtil.getCanonicalURL(completeURL, themeDisplay, layout, forceLayoutFriendlyURL); String expectedGroupFriendlyURL = StringPool.BLANK; if (!group.isGuest()) { expectedGroupFriendlyURL = group.getFriendlyURL(); } String expectedPortalDomain = virtualHostname; if (StringUtil.equalsIgnoreCase(virtualHostname, "localhost") && !StringUtil.equalsIgnoreCase(portalDomain, "localhost")) { expectedPortalDomain = portalDomain; } String expectedCanonicalURL = generateURL( expectedPortalDomain, StringPool.BLANK, expectedGroupFriendlyURL, expectedLayoutFriendlyURL); Assert.assertEquals(expectedCanonicalURL, actualCanonicalURL); }
public String getCompleteURL(HttpServletRequest request) { StringBuffer sb = request.getRequestURL(); if (sb == null) { sb = new StringBuffer(); } if (request.getQueryString() != null) { sb.append(StringPool.QUESTION); sb.append(request.getQueryString()); } String proxyPath = PortalUtil.getPathProxy(); if (Validator.isNotNull(proxyPath)) { int x = sb.indexOf(Http.PROTOCOL_DELIMITER) + Http.PROTOCOL_DELIMITER.length(); int y = sb.indexOf(StringPool.SLASH, x); sb.insert(y, proxyPath); } String completeURL = sb.toString(); if (request.isRequestedSessionIdFromURL()) { HttpSession session = request.getSession(); String sessionId = session.getId(); completeURL = PortalUtil.getURLWithSessionId(completeURL, sessionId); } if (_log.isWarnEnabled()) { if (completeURL.contains("?&")) { _log.warn("Invalid url " + completeURL); } } return completeURL; }
private long _getCompanyIdByVirtualHosts(HttpServletRequest request) { String host = PortalUtil.getHost(request); if (_log.isDebugEnabled()) { _log.debug("Host " + host); } if (Validator.isNull(host) || _isVirtualHostsIgnoreHost(host)) { return 0; } try { VirtualHost virtualHost = VirtualHostLocalServiceUtil.fetchVirtualHost(host); if (virtualHost == null) { return 0; } if (virtualHost.getLayoutSetId() != 0) { LayoutSet layoutSet = null; try { ShardUtil.pushCompanyService(virtualHost.getCompanyId()); layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(virtualHost.getLayoutSetId()); } finally { ShardUtil.popCompanyService(); } if (_log.isDebugEnabled()) { _log.debug( "Company " + virtualHost.getCompanyId() + " is associated with layout set " + virtualHost.getLayoutSetId()); } request.setAttribute(WebKeys.VIRTUAL_HOST_LAYOUT_SET, layoutSet); } return virtualHost.getCompanyId(); } catch (Exception e) { _log.error(e, e); } return 0; }
@Test public void testChildLayoutFriendlyURL() throws Exception { ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(); UserGroup userGroup = UserGroupLocalServiceUtil.addUserGroup( TestPropsValues.getUserId(), TestPropsValues.getCompanyId(), "Test " + RandomTestUtil.nextInt(), StringPool.BLANK, serviceContext); _group = userGroup.getGroup(); Layout homeLayout = LayoutLocalServiceUtil.addLayout( serviceContext.getUserId(), _group.getGroupId(), true, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "Home", StringPool.BLANK, StringPool.BLANK, LayoutConstants.TYPE_PORTLET, false, StringPool.BLANK, serviceContext); LayoutLocalServiceUtil.addLayout( serviceContext.getUserId(), _group.getGroupId(), true, homeLayout.getLayoutId(), "Child Layout", StringPool.BLANK, StringPool.BLANK, LayoutConstants.TYPE_PORTLET, false, StringPool.BLANK, serviceContext); String actualURL = PortalUtil.getActualURL( userGroup.getGroup().getGroupId(), true, Portal.PATH_MAIN, "/~/" + userGroup.getUserGroupId() + "/child-layout", new HashMap<String, String[]>(), getRequestContext()); Assert.assertNotNull(actualURL); try { PortalUtil.getActualURL( userGroup.getGroup().getGroupId(), true, Portal.PATH_MAIN, "/~/" + userGroup.getUserGroupId() + "/non-existing-child-layout", new HashMap<String, String[]>(), getRequestContext()); Assert.fail(); } catch (NoSuchLayoutException nsle) { } }
/** * Teleports the passed in Entity through the portal. * * <p>Given an Entity object, attempts to teleport them through the portal. This involves many * steps. 1) Verify the portal on the other end still exists. If it doesn't, mark this as such. 2) * If there is no counterpart, figure out where it would be, and get it. This may involve * generating and placing a portal into the world. 3) Assuming we now have a counterpart, figure * out where to teleport the entity to. 3a) Figure out the entity's position relative to the entry * portal. 3b) Translate this to a position relative to the exit portal. 3c) Preserve the entity's * camera's orientation relative to the portal. 4) Teleport the entity. 4a) If the entity is a * Player in a vehicle, we do a dance. - Raise the destination by 1 (vehicles have to 'fall' into * the portal to avoid losing momentum, so they should be one higher). - Make the player leave the * vehicle. - Spawn a new minecart at the destination. - Teleport the player to the destination. - * Make the player a passenger of the minecart. - Give the new minecart the (properly translated) * velocity of the old vehicle. - Remove the old vehicle. * * @param e The entity to teleport. * @return The location the entity was teleported to, or null if the entity was not teleported. */ public Location teleport(Entity e, Location interaction) { if (this.counterpart != null) { if (!this.counterpart.isValid()) { PortalUtil.removePortal(this.counterpart); this.counterpart = null; PortalUtil.getCounterpartPortalFor(this); } else { BlockData bd = this.getWorldBlockType(); if (bd != null && !bd.m.equals(Material.AIR) && !bd.m.equals(Material.OBSIDIAN) && !this.counterpart .getKeyBlock() .getWorld() .equals(PortalUtil.getDestWorldFor(this))) { // Did my keyblock change, and if so, change my destination. this.counterpart = null; PortalUtil.getCounterpartPortalFor(this); } } } else { PortalUtil.getCounterpartPortalFor(this); } if (this.counterpart == null) { // Could not establish a link, for whatever reason. return null; } double destX, destY, destZ; float destPitch, destYaw; int rotateVehicleVelocity = 0; Vector offset = interaction.toVector().subtract(this.keyBlock.getLocation().toVector()); Vector finalOffset; if (this.facingNorth) { if (offset.getX() < .5) { // Player moving south. offset.setX(offset.getX() + OFFSET); } else { // Player moving north. offset.setX(offset.getX() - OFFSET); } if (this.counterpart.isFacingNorth()) { destYaw = e.getLocation().getYaw(); finalOffset = offset; } else { destYaw = e.getLocation().getYaw() - 90; finalOffset = new Vector(offset.getZ(), offset.getY(), -offset.getX() + OFFSET); rotateVehicleVelocity = 1; } } else { if (offset.getZ() < .5) { // Player moving west offset.setZ(offset.getZ() + OFFSET); } else { // Player moving east. offset.setZ(offset.getZ() - OFFSET); } if (this.counterpart.isFacingNorth()) { destYaw = e.getLocation().getYaw() + 90; finalOffset = new Vector(-offset.getZ() + OFFSET, offset.getY(), offset.getX()); rotateVehicleVelocity = 2; } else { destYaw = e.getLocation().getYaw(); finalOffset = offset; } } World destWorld = this.counterpart.getKeyBlock().getWorld(); String permission = "nethrar.block." + destWorld.getName(); if ((Nethrar.getPlugin().shouldUsePermissions()) && ((e instanceof Player)) && (((Player) e).hasPermission(permission)) && !((Player) e).isOp()) { return null; } destX = this.counterpart.getKeyBlock().getX() + finalOffset.getX(); destY = this.counterpart.getKeyBlock().getY() + finalOffset.getY(); destZ = this.counterpart.getKeyBlock().getZ() + finalOffset.getZ(); destPitch = e.getLocation().getPitch(); // Jitter the location just a bit so the resulting minecart doesn't // end up underground, if there is a minecart being teleported. if ((e instanceof Player && ((Player) e).isInsideVehicle()) || (e instanceof Vehicle && !(e instanceof Pig))) { // +.11 is necessary to get a minecart to spawn on top of, instead // of inside, rails on the same level on the other side. However, // if there are *not* rails on the other side, then the minecart // will fall into the block underneath, unless a +1 is added. destY += 1.0; } Location dest; dest = new Location(destWorld, destX, destY, destZ, destYaw, destPitch); // Bug: Player camera orientation not preserved when teleporting // in a vehicle. Probably because vehicle takes over player // camera. Vehicle oldV = null, newV = null; if (e instanceof Player) { if (((Player) e).isInsideVehicle()) { oldV = (Vehicle) ((Player) e).getVehicle(); ((Player) e).leaveVehicle(); } } else if (e instanceof StorageMinecart || e instanceof Minecart || e instanceof Boat) { oldV = ((Vehicle) e); } if (oldV != null) { if (oldV instanceof StorageMinecart) { newV = destWorld.spawn(dest, StorageMinecart.class); ((StorageMinecart) newV) .getInventory() .setContents(((StorageMinecart) oldV).getInventory().getContents()); } else if (oldV instanceof Minecart) { newV = destWorld.spawn(dest, Minecart.class); } else if (oldV instanceof Boat) { newV = destWorld.spawn(dest, Boat.class); } else { log.warning("[NETHRAR] Unsupported vehicle hit a portal."); } Vector oldVelocity = oldV.getVelocity(); Vector newVelocity; switch (rotateVehicleVelocity) { // Left-handed system - clockwise is positive. case 1: // In a north-facing portal, out a west-facing portal. // Rotate 90 degrees counterclockwise. newVelocity = new Vector(oldVelocity.getZ(), oldVelocity.getY(), oldVelocity.getX() * -1); break; case 2: // In a west-facing portal, out a north-facing portal. // Rotate 90 degrees clockwise. newVelocity = new Vector(oldVelocity.getZ() * -1, oldVelocity.getY(), oldVelocity.getX()); break; default: newVelocity = oldVelocity; break; } PortalUtil.markTeleported(e); Bukkit.getServer() .getScheduler() .scheduleSyncDelayedTask( PortalUtil.getPlugin(), new NethrarTeleporter(e, dest, newV, newVelocity, oldV)); } else { PortalUtil.markTeleported(e); // Regular player teleportation doesn't need to be delayed. NethrarTeleporter tp = new NethrarTeleporter(e, dest); tp.run(); } return null; }
protected void iteratePortlets( TreeNodeView parentNodeView, PortletCategory portletCategory, Set<String> portletIds, int parentNodeId, int depth) { List<Portlet> portlets = new ArrayList<>(); String externalPortletCategory = null; for (String portletId : portletIds) { Portlet portlet = PortletLocalServiceUtil.getPortletById(_user.getCompanyId(), portletId); if (portlet != null) { if (portlet.isSystem()) { } else if (!portlet.isActive()) { } else if (portlet.isInstanceable() && !_includeInstanceablePortlets) { } else if (!portlet.isInstanceable() && _layoutTypePortlet.hasPortletId(portlet.getPortletId())) { portlets.add(portlet); } else if (!portlet.hasAddPortletPermission(_user.getUserId())) { } else { portlets.add(portlet); } PortletApp portletApp = portlet.getPortletApp(); if (portletApp.isWARFile() && Validator.isNull(externalPortletCategory)) { PortletConfig portletConfig = PortletConfigFactoryUtil.create(portlet, _servletContext); ResourceBundle resourceBundle = portletConfig.getResourceBundle(getLocale()); externalPortletCategory = ResourceBundleUtil.getString(resourceBundle, portletCategory.getName()); } } } portlets = ListUtil.sort(portlets, new PortletTitleComparator(getLocale())); for (int i = 0; i < portlets.size(); i++) { Portlet portlet = portlets.get(i); TreeNodeView nodeView = new TreeNodeView(++_nodeId); nodeView.setDepth(depth); nodeView.setLeaf(true); if ((i + 1) == portlets.size()) { nodeView.setLs("1"); } else { nodeView.setLs("0"); } nodeView.setName(PortalUtil.getPortletTitle(portlet, _servletContext, getLocale())); nodeView.setObjId(portlet.getRootPortletId()); nodeView.setParentId(parentNodeId); if (_hierarchicalTree) { parentNodeView.addChild(nodeView); } else { _list.add(nodeView); } } }