I am wondering why the method getResource keeps returning null, I have the following setup:
public static URL getResource(String path){
URL url = ResourceLoader.class.getResource(path);
if (Parameters.DEBUG){
System.out.println(path);
}
return url;
}
My project structure in Eclipse is as follows:
-- res
-- img
The path variable I pass to getResource has the value "/res/img" or "/res/img/smile.png". Yet the method keeps returning null and url is not set. I also followed the instructions of this question, which were to add the folder to the project's classpath via Run configurations, still without success... Does anyone know what I am doing wrong?
Short answer: Use "/img/smile.png".
What's actually happening is that any path starting with / which is given to the Class.getResource method is always treated as being relative to each entry in the classpath.
As your screenshot shows, the res directory is such a classpath entry. So the Class.getResource method treats the path you provide as relative to that entry. Meaning, relative to the res directory.
So, the method combines your string argument with that directory, which results in res/res/img/smile.png. Since no file (resource) exists at that location, it returns null.
http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String)
The rules for searching resources associated with a given class are
implemented by the defining class loader of the class. This method
delegates to this object's class loader. If this object was loaded by
the bootstrap class loader, the method delegates to
ClassLoader.getSystemResource(java.lang.String). Before delegation, an
absolute resource name is constructed from the given resource name
using this algorithm:
If the name begins with a '/' ('\u002f'), then the absolute name of
the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:
modified_package_name/name Where the modified_package_name is the
package name of this object with '/' substituted for '.' ('\u002e').
Related
The request mapping for my controller is something like this:
/hospital/{hospitalId}/department/{departmentId}/doctors
And i tried to add the pattern for authentication required filter:
/hospital/*/department/*/doctors
But it's not working. It's there a chance to make this work?
The mapping for a filter isn't a Ant-style mapping as you're used to in Spring, but a mapping as defined in the Servlet specification. In section 12.2 it says:
the following syntax is used to define mappings:
A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.
A string beginning with a ‘*.’ prefix is used as an extension mapping.
The empty string ("") is a special URL pattern that exactly maps to the
application's context root, i.e., requests of the form http://host:port//. In this case the path info is ’/’ and the servlet path and context path is empty string (““).
A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
All other strings are used for exact matches only.
/hospital/*/department/*/doctors only meets the criteria of the final bullet so it's treated as an exact match.
The best that you can do within confines of the servlet specification is to use /hospital/* and then do some secondary matching in your filter's code. You could use Spring Framework's org.springframework.util.AntPathMatcher to do so.
i have a webscript where i try to get two parameters filename and content passed on the url when i call this webscript but when i call the webscript whith this url http://localhost:8080/alfresco/service/fr/starxpert/workflows-repository/create-save-workflow-files?filename=testFileName="testFileName"&&content="testContent" i get this error:
The Web Script /alfresco/service/fr/starxpert/workflows-repository/create-save-workflow-files has responded with a status of 404 - Not Found.
404 Description: Requested resource is not available.
Message: 06280086 Script url /fr/starxpert/workflows-repository/create-save-workflow-files does not map to a Web Script.
There is my webscript files:
createAndSaveWorkflowFile.get.desc.xml:
<webscript>
<shortname>Creation and save new workflow file</shortname>
<description>create an workflow JSON file and save it into StarXpert Workflow repository</description>
<url>/fr/starxpert/workflows-repository/create-save-workflow-files/{filename}/{content}</url>
<format default="json">extension</format>
<authentication>user</authentication>
<family>StarXpert</family>
</webscript>
createAndSaveWorkflowFile.get.js:
var fileName=args.filename;
var properties=args.content;
logger.log(fileName);
model.filename=fileName;
model.properties=properties;
createAndSaveWorkflowFile.get.json.ftl:
{
"arguments":[
"fileName":"${fileName}",
"properties":"${properties}"
]
}
Can you tell me what i make wrong, or take me an example to how call the webscript using two arguments on the url, and then get these argument on the webscript.
You have declared that your URL uses path arguments:
/fr/starxpert/workflows-repository/create-save-workflow-files/{filename}/{content}
But when you are invoking your script you are using query string arguments:
/alfresco/service/fr/starxpert/workflows-repository/create-save-workflow-files?filename=testFileName="testFileName"&&content="testContent"
Your controller assumes query string arguments, so I will assume that's what you intend.
Further, your URL looks off.
To use query string arguments your URL should be:
/alfresco/service/fr/starxpert/workflows-repository/create-save-workflow-files?filename=testFileName&content=testContent
If you want your descriptor to match that, you need to change it to:
<url>/fr/starxpert/workflows-repository/create-save-workflow-files?filename={filename}&content={content}</url>
Notice that the ampersand is escaped so that the XML remains valid.
If you have a folder, change the display to a content item, select a page as the default view and use the method context.absolute_url_path() on the folder object, it is incorrect.
When on the context of the folder and when this method is called:
context.absolute_url_path()
It will return /my-folder/my-default-page-object
The absolute_url_path of the context of the folder should just return /my-folder.
Is there some way to get the real path of a object you are on when a default page is set?
Additional Notes:
I've tried to subtract the context.getDefaultPage() from the path, the problem is getDefaultPage on the plone site object (aka the root) keeps returning None which makes no sense since a default page is set. I've confirmed with with plones out-of-the-box Page dexterity object and a custom dexterity object.
I'm running Plone 5.
In the case you're describing, context is the default page object -- not it's containing folder. You want the url path of the "canonical" object, which is the current object unless that object is a default view. In the default view case, it's the containing folder.
To get the url path of the canonical object, use the plone_context_state view:
context_state = getMultiAdapter((context, request), name=u'plone_context_state')
canonical_object = context_state.canonical_object()
canonical_url_path = canonical_object.absolute_url_path()
If you're working in a page template, that would be:
context/##plone_context_state/canonical_object/absolute_url_path
And, if you're after the URL, not just it's path component, use absolute_url rather than absolute_url_path.
I'm working on a webapp, one function of which was to list all the files under given path. I tried to map several segments of URL to one PathVariable like this :
#RequestMapping("/list/{path}")
public String listFilesUnderPath(#PathVariable String path, Model model) {
//.... add the file list to the model
return "list"; //the model name
}
It didn't work. When the request url was like /list/folder_a/folder_aa, RequestMappingHandlerMapping complained : "Did not find handler method for ..."
Since the given path could contains any number of segments, it's not practical to write a method for every possible situation.
In REST each URL is a separate resource, so I don't think you can have a generic solution. I can think of two options
One option is to change the mapping to #RequestMapping("/list/**") (path parameter no longer needed) and extract the whole path from request
Second option is to create several methods, with mappings like #RequestMapping("/list/{level1}"), #RequestMapping("/list/{level1}/{level2}"), #RequestMapping("/list/{level1}/{level2}/{level3}")... concatenate the path in method bodies and call one method that does the job. This, of course, has a downside that you can only support a limited folder depth (you can make a dozen methods with these mappings if it's not too ugly for you)
You can capture zero or more path segments by appending an asterisk to the path pattern.
From the Spring documentation on PathPattern:
{*spring} matches zero or more path segments until the end of the path and captures it as a variable named "spring"
Note that the leading slash is part of the captured path as mentioned in the example on the same page:
/resources/{*path} — matches all files underneath the /resources/, as well as /resources, and captures their relative path in a variable named "path"; /resources/image.png will match with "path" → "/image.png", and /resources/css/spring.css will match with "path" → "/css/spring.css"
For your particular problem the solution would be:
#RequestMapping("/list/{*path}") // Use *path instead of path
public String listFilesUnderPath(#PathVariable String path, Model model) {
//.... add the file list to the model
return "list"; //the model name
}
I've seen similar questions and answers regarding conversions from virtual to absolute and url, but how can I convert a url to a virtual path without manual string parsing?
Example:
I want "http://myserver/home.aspx" converted to: "~/home.aspx"
I realize the above example would be an easy string parsing routine, but I'm looking for a proper solution that will scale to the changing of the url format.
You can get most of it from the Uri class:
new Uri("http://myserver.com/home.aspx").AbsolutePath
Then you just have to prepend the ~
Though, that will might break if you host in a subdirectory - I don't think there's a way to do it specifically in the context of the application you're running.
EDIT: This might do it:
VirtualPathUtility.ToAppRelative(new Uri("http://myserver.com/home.aspx").AbsolutePath);
VirtualPathUtility.ToAppRelative Method (String) seems to be what you are looking for (http://msdn.microsoft.com/en-us/library/ms150163.aspx)
If the virtual path for the application is "myapp" and the virtual path "/myApp/sub/default.asp" is passed into the ToAppRelative method, the resulting application-relative path is "~/sub/default.aspx".