We recently upgraded the application from Wicket 1.5.8 to 6.22.0. I am trying to set all pages to redirect to a specific page (SessionExpiredPage) when an action occurs after the session has expired.
public class WicketApplication<HttpsRequestCycleProcessor> extends WebApplication
{
// other methods omitted
public void init()
{
super.init();
getApplicationSettings().setPageExpiredErrorPage(SessionExpiredPage.class);
getApplicationSettings().setAccessDeniedPage(SessionExpiredPage.class);
getPageSettings().setRecreateMountedPagesAfterExpiry(false);
// several other mounted links omitted
mount(new MountedMapper("landingpage", LandingPage.class, new UrlPathPageParametersEncoder()));
// add your configuration here
getComponentInstantiationListeners().add(new SpringComponentInjector(this));
}
}
Setting setRecreateMountedPagesAfterExpiry to false helps redirect several pages upon session expiry, however there is an unintended consequence. The application contains several servlet pages. One of them is accessed from a wicket page like this:
#RequireHttps
public class SubscriptionPage extends WebPage
{
#Override
public void onSubmit()
{
String redirectUrl = null;
// condition checking code omitted
redirectUrl= "SubsCartTempServlet?subsunit=6";
// more code omitted
getRequestCycle().scheduleRequestHandlerAfterCurrent(new RedirectRequestHandler(redirectUrl));
}
}
The url is changed from http://localhost:8080/LatinParserK/SubsCartTempServlet?subsunit=6, which worked, to
http://localhost:8080/LatinParserK/wicket/SubsCartTempServlet?subsunit=6, which fails.
Can anyone explain how to work around this problem?
It seems the path to the Wicket page and to the Servlet have different depth.
Use org.apache.wicket.request.UrlUtils#rewriteToContextRelative(relativeUrl, cycle) to make the url to the Servlet relative to the context root.
The last line under onSubmit() was replaced with:
String relativeUrl = UrlUtils.rewriteToContextRelative(redirectUrl, getRequestCycle());
getRequestCycle().scheduleRequestHandlerAfterCurrent(new RedirectRequestHandler(relativeUrl));
That corrected the URL to the servlet.
Related
We are trying to provide a clean URI structure for external endpoints to pull json information from CQ5.
For example, if you want to fetch information about a particular users history (assuming you have permissions etc), ideally we would like the endpoint to be able to do the following:
/bin/api/user/abc123/phone/555-klondike-5/history.json
In the URI, we would specifying /bin/api/user/{username}/phone/{phoneNumber}/history.json so that it is very easy to leverage the dispatcher to invalidate caching changes etc without invalidating a broad swath of cached information.
We would like to use a sling servlet to handle the request, however, I am not aware as to how to put variables into the path.
It would be great if there were something like #PathParam from JaxRS to add to the sling path variable, but I suspect it's not available.
The other approach we had in mind was to use a selector to recognise when we are accessing the api, and thus could return whatever we wanted to from the path, but it would necessitate a singular sling servlet to handle all of the requests, and so I am not happy about the approach as it glues a lot of unrelated code together.
Any help with this would be appreciated.
UPDATE:
If we were to use a OptingServlet, then put some logic inside the accepts function, we could stack a series of sling servlets on and make the acceptance decisions from the path with a regex.
Then during execution, the path itself can be parsed for the variables.
If the data that you provide comes from the JCR repository, the best is to structure it exactly as you want the URLs to be, that's the recommended way of doing things with Sling.
If the data is external you can create a custom Sling ResourceProvider that you mount on the /bin/api/user path and acquires or generates the corresponding data based on the rest of the path.
The Sling test suite's PlanetsResourceProvider is a simple example of that, see http://svn.apache.org/repos/asf/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/
The Sling resources docs at https://sling.apache.org/documentation/the-sling-engine/resources.html document the general resource resolution mechanism.
It is now possible to integrate jersy(JAX-RS) with CQ. We are able to create primitive prototype to say "Hello" to the world.
https://github.com/hstaudacher/osgi-jax-rs-connector
With this we can use the #PathParam to map the requests
Thanks and Regards,
San
There is no direct way to create such dynamic paths. You could register servlet under /bin/api/user.json and provide the rest of the path as a suffix:
/bin/api/user.json/abc123/phone/555-klondike-5/history
^ ^
| |
servlet path suffix starts here
then you could parse the suffix manually:
#SlingServlet(paths = "/bin/api/user", extensions = "json")
public class UserServlet extends SlingSafeMethodsServlet {
public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
String suffix = request.getRequestPathInfo().getSuffix();
String[] split = StringUtils.split(suffix, '/');
// parse split path and check if the path is valid
// if path is not valid, send 404:
// response.sendError(HttpURLConnection.HTTP_NOT_FOUND);
}
}
The RESTful way to approach this would be to have the information stored in the structure that you want to use. i.e. /content/user/abc123/phone/555-klondike-5/history/ would contain all the history nodes for that path.
In that usage. you can obtain an out of the box json response by simply calling
/content/user/abc123/phone/555-klondike-5/history.json
Or if you need something in a specific json format you could use the sling resource resolution to use a custom json response.
Excited to share this! I've worked ~ a week solving this, finally have the best Answer.
First: Try to use Jersey
The osgi-jax-rs-connector suggested by kallada is best, but I couldn't get it working on Sling 8. I lost a full day trying, all I have to show for it are spooky class not found errors and dependency issues.
Solution: The ResourceProvider
Bertrand's link is for Sling 9 only, which isn't released. So here's how you do it in Sling 8 and older!
Two Files:
ResourceProvider
Servlet
The ResourceProvider
The purpose of this is only to listen to all requests at /service and then produce a "Resource" at that virtual path, which doesn't actually exist in the JCR.
#Component
#Service(value=ResourceProvider.class)
#Properties({
#Property(name = ResourceProvider.ROOTS, value = "service/image"),
#Property(name = ResourceProvider.OWNS_ROOTS, value = "true")
})
public class ImageResourceProvider implements ResourceProvider {
#Override
public Resource getResource(ResourceResolver resourceResolver, String path) {
AbstractResource abstractResource;
abstractResource = new AbstractResource() {
#Override
public String getResourceType() {
return TypeServlet.RESOURCE_TYPE;
}
#Override
public String getResourceSuperType() {
return null;
}
#Override
public String getPath() {
return path;
}
#Override
public ResourceResolver getResourceResolver() {
return resourceResolver;
}
#Override
public ResourceMetadata getResourceMetadata() {
return new ResourceMetadata();
}
};
return abstractResource;
}
#Override
public Resource getResource(ResourceResolver resourceResolver, HttpServletRequest httpServletRequest, String path) {
return getResource(resourceResolver , path);
}
#Override
public Iterator<Resource> listChildren(Resource resource) {
return null;
}
}
The Servlet
Now you just write a servlet which handles any of the resources coming from that path - but this is accomplished by handling any resources with the resource type which is produced by the ResourceProvider listening at that path.
#SlingServlet(
resourceTypes = TypeServlet.RESOURCE_TYPE,
methods = {"GET" , "POST"})
public class TypeServlet extends SlingAllMethodsServlet {
static final String RESOURCE_TYPE = "mycompany/components/service/myservice";
#Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
final String [] pathParts = request.getResource().getPath().split("/");
final String id = pathParts[pathParts.length-1];
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
out.print("<html><body>Hello, received this id: " + id + "</body></html>");
} finally {
out.close();
}
}
}
Obviously your servlet would do something much more clever, such as process the "path" String more intelligently and probably produce JSON.
How to restrict a CQ5/Custom component to add only once per page.? I want to restrict the drag and drop of component into the page when the author is going to add the same component for the second time into the same page.
One option is to include the component directly in the JSP of the template and exclude it from the list of available components in the sidekick. To do so, add the component directly to your JSP (foundation carousel in this example):
<cq:include path="carousel" resourceType="foundation/components/carousel" />
To hide the component from the sidekick, either set:
componentGroup: .hidden
or exclude it from the list of "Allowed Components" using design mode.
If you need to allow users to create a page without this component you can provide a second template with the cq:include omitted.
Thanks Rampant, I have followed your method and link stated.
Posting link again : please follow this blog
It was really helpful. I am posting the implementation whatever I have done.
It worked fine for me. One can definitely improve the code quality, this is raw code and is just for reference.
1.Servlet Filter
Keep this in mind that,if any resource gets refereshed, this filter will execute. So you need to filter the contents at your end for further processing.
P.S. chain.doFilter(request,response); is must. or cq will get hanged and nothing will be displayed.
#SlingFilter(generateComponent = false, generateService = true, order = -700,
scope = SlingFilterScope.REQUEST)
#Component(immediate = true, metatype = false)
public class ComponentRestrictorFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {}
#Reference
private ResourceResolverFactory resolverFactory;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
WCMMode mode = WCMMode.fromRequest(request);
if (mode == WCMMode.EDIT) {
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
PageManager pageManager = slingRequest.getResource().getResourceResolver().adaptTo(PageManager.class);
Page currentPage = pageManager.getContainingPage(slingRequest.getResource());
logger.error("***mode" + mode);
if (currentPage != null )) {
ComponentRestrictor restrictor = new ComponentRestrictor(currentPage.getPath(), RESTRICTED_COMPONENT);
restrictor.removeDuplicateEntry(resolverFactory,pageManager);
}
chain.doFilter(request, response);
}
}
public void destroy() {}
}
2.ComponentRestrictor class
public class ComponentRestrictor {
private String targetPage;
private String component;
private Pattern pattern;
private Set<Resource> duplicateResource = new HashSet<Resource>();
private Logger logger = LoggerFactory.getLogger(ComponentRestrictor.class);
private Resource resource = null;
private ResourceResolver resourceResolver = null;
private ComponentRestrictorHelper helper = new ComponentRestrictorHelper();
public ComponentRestrictor(String targetPage_, String component_){
targetPage = targetPage_ + "/jcr:content";
component = component_;
}
public void removeDuplicateEntry(ResourceResolverFactory resolverFactory, PageManager pageManager) {
pattern = Pattern.compile("([\"']|^)(" + component + ")(\\S|$)");
findReference(resolverFactory, pageManager);
}
private void findReference(ResourceResolverFactory resolverFactory, PageManager pageManager) {
try {
resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
resource = resourceResolver.getResource(this.targetPage);
if (resource == null)
return;
search(resource);
helper.removeDuplicateResource(pageManager,duplicateResource);
} catch (LoginException e) {
logger.error("Exception while getting the ResourceResolver " + e.getMessage());
}
resourceResolver.close();
}
private void search(Resource parentResource) {
searchReferencesInContent(parentResource);
for (Iterator<Resource> iter = parentResource.listChildren(); iter.hasNext();) {
Resource child = iter.next();
search(child);
}
}
private void searchReferencesInContent(Resource resource) {
ValueMap map = ResourceUtil.getValueMap(resource);
for (String key : map.keySet()) {
if (!helper.checkKey(key)) {
continue;
}
String[] values = map.get(key, new String[0]);
for (String value : values) {
if (pattern.matcher(value).find()) {
logger.error("resource**" + resource.getPath());
duplicateResource.add(resource);
}
}
}
}
}
3.To remove the node/ resource
Whichever resource you want to remove/delete just use PageManager api
pageManeger.delete(resource,false);
That's it !!! You are good to go.
None of the options looks easy to implement. The best approach I found is to use the ACS Commons Implementation which is very easy and can be adopted into any project.
Here is the link and how to configure it:
https://github.com/Adobe-Consulting-Services/acs-aem-commons/pull/639
Enjoy coding !!!
you can't prevent that without doing some massive hacking to the ui code, and even then, you've only prevented it from one aspect of the ui. there's still crxde, and then the ability to POST content.
if this is truly a requirement, the best approach might be the following:
have the component check for a special value in the pageContext object (use REQUEST_SCOPE)
if value is not found, render component and set value
otherwise, print out a message that component can only be used once
note that you can't prevent a dialog from showing, but at the very least the author has an indication that that particular component can only be used once.
It sounds like there needs to be clarification of requirements (and understanding why).
If the authors can be trained, let them manage limits of components through authoring and review workflows.
If there is just 1 fixed location the component can appear, then the page component should include the content component, and let the component have an "enable" toggle property to determine if it should render anything. The component's group should be .hidden to prevent dragging from the sidekick.
If there is a fixed set of locations for the component, the page component can have a dropdown of the list of locations (including "none"). The page render component would then conditionally include the component in the correct location. Again, prevent dragging the component from the sidekick.
In the "hard to imagine" case that the component can appear anywhere on the page, added by authors, but limited to only 1 instance - use a wrapper component to manage including the (undraggable) component. Let the authors drag the wrapper on the page as many times as they want, but the wrapper should query the page's resources and determine if it is the first instance, and if so, include the end component. Otherwise, the wrapper does nothing.
In our experience (>2years on CQ), implementing this type of business rules via code creates a brittle solution. Also, requirements have a habit of changing. If enforced via code, development work is required instead of letting authors make changes faster & elegantly.
None of these options are that great. If you truly want a robust solution to this problem (limit the number of items on the page without hardcoding location) then the best way is with a servlet filter chain OSGI service where you can administer the number of instances and then use a resource resolver to remove offending instances.
The basic gist is:
Refresh the page on edit using cq:editConfig
Create an OSGI service implementing javax.servlet.Filter that encapsulates your business rules.
Use the filter to remove excess components according to business rules
Continue page processing.
For more details see here:
Using a servlet filter to limit the number of instances of a component per page or parsys
This approach will let you administer the number of items per page or per parsys and apply other possibly complex business rules in a way that the other offered solutions simply cannot.
I want to route HTTP requests not to an action, but to a file.
Important: I do have a working solution using IIS 7.0 URL Rewriting module, but for debugging at home (no IIS 7.0) I can't use URL rewriting.
Specific situation
I want to point any URL that contains /images/ to a ~/images/ folder.
Example:
http://wowreforge.com/images/a.png -> /images/a.png
http://wowreforge.com/Emerald Dream/Jizi/images/a.png -> /images/a.png
http://wowreforge.com/US/Emerald Dream/Jizi/images/a.png -> /images/a.png
http://wowreforge.com/characters/view/images/a.png -> /images/a.png
The problem stems from the fact that page "view_character.aspx" can be arrived to from multiple URLs:
http://wowreforge.com/?name=jizi&reaml=Emerald Dream
http://wowreforge.com/US/Emerald Dream/Jizi
Context
IIS 7.0 (integrated mode), ASP.NET MVC 2.0
Extra Credit Questions
Is it a bad idea to use ASP.NET MVC routing in this situation instead of URL rewriting?
What handler does IIS 7.0 routes requests to physical files?
You should probably rewrite your links to images to.
<img src="<%= ResolveUrl("~/images/a.png") %>" />
That way you don't need to have your routes handle the images.
UPDATE
How you would do it through routing
add this entry to your RouteTable
routes.Add("images", new Route("{*path}", null,
new RouteValueDictionary(new { path = ".*/images/.*"}),
new ImageRouteHandler()));
Now you need to create an ImageRouteHandler and an ImageHandler
public class ImageRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
//you'll need to figure out how to get the physical path
return new ImageHandler(/* get physical path */);
}
}
public class ImageHandler : IHttpHandler
{
public string PhysicalPath { get; set; }
public ImageHandler(string physicalPath)
{
PhysicalPath = physicalPath;
}
public void ProcessRequest(HttpContext context)
{
context.Response.TransmitFile(PhysicalPath);
}
public bool IsReusable
{
get { return true; }
}
}
This also doesn't do any caching. You can check out System.Web.StaticFileHandler in Reflector for the handler that processes static files for an Asp.Net application for a more complete implementation.
When I call other action in one action, it also display itself template, in Play 1.1 RC
and when I Redirect("...url") but it does not work, is there someone that can help me?
Just to add to the answers above, here's how you redirect to an external url:
public static void index() {
redirect("http://geeks.aretotally.in");
}
To redirect, you simply call the action. From the example in the documentation:
public static void show(Long id) {
Article article = Article.findById(id);
render(article);
}
public static void edit(Long id, String title) {
Article article = Article.findById(id);
article.title = title;
article.save();
show(id);
}
At the end of the edit action, the call to show(...) will cause a redirect on the client's browser as if they had hit the same URL that routes to the show method.
Since none of these answers provide a general/reusable method to do this, here is my code. This allows you to create any number of redirects in the conf/routes file without creating a controller for each.
Yes, this is trivial, but perhaps it is of use to someone.
conf/routes:
GET /admin Application.redirect(url:'/admin/index.html')
app/controllers/Application.java:
public class Application extends Controller {
public static void redirect(String url) {
redirect(url, true);
}
}
In the play framework, when you call an action, by default it renders the template associated with that action.
For example, a Contoller named Application
public static void index()
Will render
app/views/Application/index.html
To make it render a different view, then you can specify the template as the first parameter in the render method.
So,
renderTemplate("Application/myOtherTemplate.html");
Redirect should only really be used if you are redirecting to a URL outside of your application.
The web designer has given me HTML which looks like:
<div .... style="background: transparent url(xxx.png) 170px center no-repeat">
Unfortunately the contents of the image xxx.png is generated by the software, so I have made it a WebResource and use the following strategy to generate the URL for the resource which I then embed in the style= attribute using a Wicket AttributeModifier.
// App initialization code
String resourceName = ....;
getSharedResources().add(resourceName, myWebResource);
// Creating the widget
String url = getServletContext().getContextPath()
+ "/resources/org.apache.wicket.Application/" + resourceName ;
String style = "background: transparent url(" + url + ") 170px center no-repeat";
div.add(new AttributeModifier("style", new Model<String>(style)));
This works fine when I test it locally using Eclipse, but :
When I install this in production, I want to have Apache as a proxy to Jetty such that the context root isn't visible, i.e. Apache forwards a request of /foo onto Jetty as /context-root/foo.
In general, I don't think this is very elegant. I'm sure I am duplicating Wicket code here?
I understand Wicket solves this problem of context-roots and Apache proxying by only using relative URLs. That would be the most elegant solution I suspect. But if I have e.g. a IndexedParamUrlCodingStrategy then the URL could be of arbitrary length and I don't know how many .. to include to get back to /resources.
Edit: The current solution is to use absolute URLs as in my code example above, and in Apache (a) rewrite /context-root/* into /* (b) as before then ADD the context root to all requests (c) forward to Jetty. That way most URLs can be without the context root but some URLs (to my resources) can have the context root and it's OK. But I don't like this solution!
If the code is called from inside a component (or page):
urlFor(new ResourceReference("sharedResourceName"));
or
RequestCycle.get().urlFor(new ResourceReference("sharedResourceName"));
Sample application below. I used a ByteArrayResource for simplicity, but any Resource subclass will do:
WicketApplication.java
package app1;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.target.coding.IndexedParamUrlCodingStrategy;
import org.apache.wicket.resource.ByteArrayResource;
public class WicketApplication extends WebApplication {
#Override
protected void init() {
super.init();
getSharedResources().add("testResource", new ByteArrayResource("text/plain", "This is a test".getBytes()));
mount(new IndexedParamUrlCodingStrategy("home/very/deep/folder", getHomePage()));
}
public Class<HomePage> getHomePage() {
return HomePage.class;
}
}
HomePage.java
package app1;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.WebPage;
public class HomePage extends WebPage {
public HomePage(final PageParameters parameters) {
CharSequence resourceHref = urlFor(new ResourceReference("testResource"));
add(new Label("link", "Click me!")
.add(new SimpleAttributeModifier("href", resourceHref)));
}
}
HomePage.html
<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" >
<body>
<a wicket:id="link"></a>
</body>
</html>
I think the tactic used in this answer for creating dynamic image urls will apply here.