Skip to content

Commit

Permalink
Merge branch 'master' into issue-30
Browse files Browse the repository at this point in the history
* master:
  Restore use of testing tool #50
  Fixed bug in the path parsing of .get
  #43: Cleanup, removing redundant logs and comments
  #43: getCleanPath instead of contextPathOverride and prefix
  #45: In prod mode, hide (swallow) 400 and 404 error messages. Only output them in XP dev mode.
  #44: Preventing keys with undefined values in response header.
  Fixed some tests Removed tests that don't verify lib-static functionality (caching ResourceService now does it) Commented out tests that won't run on existing XP Testing Tool (need to wait for XP 7.7 Testing Tool)
  Commenting out syntax error in test (which is incomplete anyway)
  Remove double quotation handling
  Static root resolution tweak
  #38: decodeURI solves URI encoding issue. Improving double-quoted etag handling, and charset on error responses.
  Etag doublequotes handling
  CR changes
  Refactored the EtagService, moving some checks out of it to clean the responsibility and dispense with the STATUS and handling - simplifying a lot. Now tests fail, rewriting those.
  Java implementation can be improved #30
  new gradle wrapper
  Create enonic-gradle.yml

# Conflicts:
#	src/main/resources/lib/enonic/static/index.es6
#	src/test/java/lib/enonic/libStatic/etag/EtagServiceTest.java
  • Loading branch information
Espen Norderud committed Mar 19, 2021
2 parents eabad21 + 6d66b80 commit 350d74e
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 187 deletions.
116 changes: 116 additions & 0 deletions .github/workflows/enonic-gradle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
name: Gradle Build

on: [push]

jobs:
build:

runs-on: ubuntu-latest

needs: release_notes
if: always()
steps:
- uses: actions/checkout@v2.3.4

- uses: actions/setup-java@v1
with:
java-version: 11

- uses: actions/cache@v2.1.4
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: |
${{ runner.os }}-gradle-
- run: ./gradlew build

- uses: codecov/codecov-action@v1.2.1

### PUBLISHING STEPS ###

- name: Get publishing variables
id: publish_vars
uses: enonic/release-tools/publish-vars@master
env:
PROPERTIES_PATH: './gradle.properties'
JAVA_HOME: ''

- name: Fail on bad config
if: steps.publish_vars.outputs.version == '' || steps.publish_vars.outputs.projectName == ''
run: exit 1

- name: Publish
if: ${{ github.ref == 'refs/heads/master' }}
run: ./gradlew publish -PrepoKey=${{ steps.publish_vars.outputs.repo }} -PrepoUser=${{ secrets.ARTIFACTORY_USERNAME }} -PrepoPassword=${{ secrets.ARTIFACTORY_PASSWORD }}

- name: Download changelog
if: steps.publish_vars.outputs.release == 'true'
uses: actions/download-artifact@v2
with:
name: changelog

- name: Create Release
if: steps.publish_vars.outputs.release == 'true'
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.publish_vars.outputs.tag_name }}
body_path: changelog.md
prerelease: ${{ steps.publish_vars.outputs.prerelease == 'true' }}

- name: Upload Release Asset
id: upload-release-asset
if: "steps.create_release.outputs.upload_url != ''"
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: "build/libs/${{ steps.publish_vars.outputs.projectName }}-${{ steps.publish_vars.outputs.version }}.jar"
asset_name: "${{ steps.publish_vars.outputs.projectName }}-${{ steps.publish_vars.outputs.version }}.jar"
asset_content_type: application/java-archive

- name: Write new snapshot version
if: steps.publish_vars.outputs.release == 'true'
uses: christian-draeger/write-properties@1.0.1
with:
path: './gradle.properties'
property: 'version'
value: ${{ steps.publish_vars.outputs.nextSnapshot }}

- name: Commit and push new version
if: steps.publish_vars.outputs.release == 'true'
uses: EndBug/add-and-commit@v6.2.0
with:
add: ./gradle.properties
message: 'Updated to next SNAPSHOT version'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

release_notes:
runs-on: ubuntu-latest
if: "(github.ref == 'refs/heads/master')"
steps:
- uses: actions/checkout@v2.3.4
with:
fetch-depth: 0

- name: Get previous release tag
id: get_previous_release_tag
run: |
PREVIOUS_RELEASE_TAG=$(git tag --sort=-version:refname --merged | grep -E '^v([[:digit:]]+\.){2}[[:digit:]]+$' | head -1)
echo ::set-output name=previous_release_tag::$PREVIOUS_RELEASE_TAG
- name: Generate Release Notes
uses: enonic/release-tools/generate-changelog@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ZENHUB_TOKEN: ${{ secrets.ZENHUB_TOKEN }}
PREVIOS_RELEASE_TAG: ${{ steps.get_previous_release_tag.outputs.previous_release_tag }}
OUTPUT_FILE: changelog.md

- uses: actions/upload-artifact@v2
with:
name: changelog
path: changelog.md
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,7 @@ exports.get = (request) => {

The path to the actual asset is determined by the URL path (`request.path`). This relative to the access URL of the controller itself.


NOTE: It's recommended to use `.static` in an XP [service controller](https://developer.enonic.com/docs/xp/stable/runtime/engines/http-service). Here, routing is included, the endpoint's rootpath is already in `request.contextPath`, and relative asset path is automatically determined. However, if you use `.static` in a non-service controller, you must supply the endpoint's rootpath in options, as `contextPathOverride`!

For example, if _getAnyStatic.es6_ is accessed with a [controller mapping](https://developer.enonic.com/docs/xp/stable/cms/mappings) at `https://someDomain.com/resources/public`, then that's an endpoint with the rootpath `resources/public`. Since this is not a service, we add the contextPathOverride option:

`const getStatic = libStatic.static('my-resources', {contextPathOverride: 'resources/public'});`

Now the URL `https://someDomain.com/resources/public/subfolder/target-resource.xml` will return the static resource _/my-resources/subfolder/target-resource.xml_ from the JAR (a.k.a. _build/resources/main/my-resources/subfolder/target-resource.xml_ in dev mode).
NOTE: It's recommended to use `.static` in an [XP service controller](https://developer.enonic.com/docs/xp/stable/runtime/engines/http-service). Here, routing is included and easy to handle: the endpoint's standard root path is already in `request.contextPath`, and the asset path is automatically determined relative to that. If you use `.static` in a context where the asset path (relative to `root`) can't be determined by simply subtracting `request.contextPath` from the beginning of `request.rawPath`, you should add a `getCleanPath` [option parameter](#options).

Same example as above, but simplified and without options:
```
Expand Down Expand Up @@ -229,7 +222,7 @@ NOTE: mutable assets should not be served with this header! See [below](#mutable
As described above, an object can be added with optional attributes to **override** the [default behaviour](#behaviour):

```
{ cacheControl, contentType, etag, throwErrors, contextPathOverride }
{ cacheControl, contentType, etag, getCleanPath, throwErrors }
```

### Params:
Expand All @@ -246,10 +239,29 @@ As described above, an object can be added with optional attributes to **overrid
- `etag` (boolean, optional): The default behavior of lib-static is to generate/handle ETag in prod, while skipping it entirely in dev mode.
- Setting the etag parameter to `false` will turn **off** etag processing (runtime content processing, headers and handling) in **prod** too.
- Setting it to `true` will turn it **on in dev mode** too.
- `getCleanPath` (function, optional): Only used in [.static](#api-static). The default behavior of the returned `getStatic` function is to take a request object, and compare the beginning of the current requested path (`request.rawPath`) to the endpoint's own root path (`request.contextPath`) and get a relative asset path below `root` (so that later, prefixing the `root` value to that relative path will give the absolute full path to the resource in the JAR). In cases where this default behavior is not enough, you can override it by adding a `getCleanPath` param: `(request) => 'resource/path/below/root'`. Emphasis: the returned 'clean' path from this function should be _relative to the `root` folder_, not an absolute path in the JAR.
- **For example:** if _getAnyStatic.es6_ is accessed with a [controller mapping](https://developer.enonic.com/docs/xp/stable/cms/mappings) at `https://someDomain.com/resources/public`, then that's an endpoint with the path `resources/public` - but that can't be determined from the request. So the automatic extraction of a relative path needs a `getCleanPath` override. Very simplified here:
```
const getStatic = libStatic.static(
'my-resources',
{
getCleanPath: (request) => {
if (!resource.rawPath.startsWith('resources/public') { throw Error('Ooops'); }
return resource.rawPath.substring('resources/public'.length);
}
}
);
```
- Now, since `request.rawPath` doesn't include the protocol or domain, the URL `https://someDomain.com/resources/public/subfolder/target-resource.xml` will make `getCleanPath` return `/subfolder/target-resource.xml`, which together with `root` will look up the resource _/my-resources/subfolder/target-resource.xml_ in the JAR (a.k.a. _build/resources/main/my-resources/subfolder/target-resource.xml_ in dev mode).
- `throwErrors` (boolean, default is `false`): by default, the `.get` method should not throw errors when used correctly. Instead, it internally server-logs (and hash-ID-tags) errors and automatically outputs a 500 error response.
- Setting `throwErrors` to `true` overrides this: the 500-response generation is skipped, and the error is re-thrown down to the calling context, to be handled there.
- This does not apply to 404-not-found type "errors", they will always generate a 404-response either way.
- `contextPathOverride`: Only used in [.static](#api-static). The default behavior of the returned `getStatic` function is to take a request object, and compare the current path to the endpoint's rootpath to get a relative asset path (below `root` in the JAR). This is done by looking at `request.contextPath` and removing that from `request.path`. However, contextPath only works well in an XP service - if you want to use `static` elsewhere, for example with a controller mapping, you need to supply the endpoint's root path to `contextPathOverride`.
- This does not apply to 404-not-found type "errors", they will always generate a 404-response either way.
In addition, you may supply a `path` or `root` param ([.get](#api-get) or [.static](#api-static), respectively). If a positional `path` or `root` argument is used and the options object is the second argument, then `path` or `root` parameters will be ignored in the options object.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies {

repositories {
jcenter()
xp.enonicRepo()
xp.enonicRepo('dev')
}

// This task takes care of es6 under src/main/resources/site. You can replace it with build steps of your own if you want.
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
group = com.enonic.lib
projectName = lib-static
xpVersion = 7.5.0
xpVersion = 7.7.0-SNAPSHOT
version=0.0.1-SNAPSHOT
Loading

0 comments on commit 350d74e

Please sign in to comment.