-
Notifications
You must be signed in to change notification settings - Fork 834
include ref in path as WW-5011 workaround #353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
803feba
include ref in path for StrutsApplicationResource as WW-5011 workaround
yasserzamani 0adc404
add license to newly added xml files in previous commit
yasserzamani f0448a0
WW-5011 include ref in path via .toURI().getPath()
yasserzamani 9d32493
WW-5011 check protocol and not throw exception due to lazy file exist…
yasserzamani fd91e75
decrease log file size to pass travis build
yasserzamani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,19 +19,47 @@ | |
| package org.apache.struts2.tiles; | ||
|
|
||
| import org.apache.tiles.request.locale.PostfixedApplicationResource; | ||
| import org.apache.tiles.request.locale.URLApplicationResource; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.URI; | ||
| import java.net.URL; | ||
|
|
||
| public class StrutsApplicationResource extends PostfixedApplicationResource { | ||
|
|
||
| private final URL url; | ||
| private final File file; | ||
|
|
||
| /** | ||
| * fixes WW-5011 | ||
| * @return file path for "file" protocol elsewhere url path as before to keep backward-compatibility | ||
| * @see URLApplicationResource#getFile(URL) | ||
| */ | ||
| private static String getFilePath(URL url) { | ||
| String path = url.getPath(); | ||
| if (!"file".equals(url.getProtocol())) { | ||
| return path; | ||
| } | ||
| try { | ||
| // fixes WW-5011 because includes ref in path - like URLApplicationResource#getFile(URL) | ||
| path = (new URI(url.toExternalForm())).getSchemeSpecificPart(); | ||
| } catch (Exception e) { | ||
| // fallback solution | ||
| if (url.getRef() != null && !new File(path).exists()) { | ||
| // it's like WW-5011 | ||
| path += "#" + url.getRef(); | ||
| } | ||
| } | ||
|
|
||
| return path; | ||
| } | ||
|
|
||
| public StrutsApplicationResource(URL url) { | ||
| super(url.getPath()); | ||
| super(getFilePath(url)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One last suggestion 😄 for the constructor (avoids calling getFilePath twice/regenerating the path string): final String urlPath = getFilePath(url);
super(urlPath);
this.url = url;
this.file = new File(urlPath); |
||
| this.url = url; | ||
| this.file = new File(getFilePath(url)); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -41,7 +69,6 @@ public InputStream getInputStream() throws IOException { | |
|
|
||
| @Override | ||
| public long getLastModified() throws IOException { | ||
| File file = new File(url.getPath()); | ||
| if (file.exists()) { | ||
| return file.lastModified(); | ||
| } | ||
|
|
||
43 changes: 43 additions & 0 deletions
43
plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsApplicationResourceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.struts2.tiles; | ||
|
|
||
| import org.apache.tiles.request.ApplicationResource; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| import java.net.URL; | ||
|
|
||
| public class StrutsApplicationResourceTest { | ||
|
|
||
| @Test | ||
| public void testWW5011fix() throws Exception { | ||
| URL resource = getClass().getClassLoader().getResource("emptyTiles.xml"); | ||
| ApplicationResource ar = new StrutsApplicationResource(resource); | ||
| Assert.assertNotEquals(0, ar.getLastModified()); | ||
|
|
||
| resource = getClass().getClassLoader().getResource("emptyTiles###2.xml"); | ||
| ar = new StrutsApplicationResource(resource); | ||
| Assert.assertNotEquals(0, ar.getLastModified()); | ||
|
|
||
| resource = getClass().getClassLoader().getResource("emptyTiles.xml"); | ||
| ar = new StrutsApplicationResource(new URL(resource + "#ref1")); | ||
| Assert.assertNotEquals(0, ar.getLastModified()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| --> | ||
| <tiles-definitions></tiles-definitions> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| --> | ||
| <tiles-definitions></tiles-definitions> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar comment about sub-context representation and anchor/fragment "#".
A larger concern is there is now a File existence check happening here. That seems very risky as the URL can represent a non-file protocol resource.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But getLastModified method already considers it should be a file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems
getLastModified()considers the possibility that resource (path) is a file, but it doesn't assume it has to be a file.If the path doesn't exist as a file (e.g. nonexistent file or non-file URL) then
exists()will be false andgetLastModified()just returns 0. (As a side note the base Tiles interface also allows for anIOExceptionto be thrown if there's no modified date available ... but Struts has attempted to avoid that).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed :) - learnt from tiles URLApplicationResource.