-
Notifications
You must be signed in to change notification settings - Fork 160
Store enclosure metadata without fetching remote URLs #175
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
Open
snoopdave
wants to merge
2
commits into
master
Choose a base branch
from
enclosure-metadata-entry
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
154 changes: 154 additions & 0 deletions
154
app/src/main/java/org/apache/roller/weblogger/util/EnclosureMetadata.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,154 @@ | ||
| /* | ||
| * 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.roller.weblogger.util; | ||
|
|
||
| import java.net.IDN; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.util.Locale; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Validated metadata for an RSS or Atom enclosure. | ||
| */ | ||
| public final class EnclosureMetadata { | ||
|
|
||
| private static final Pattern MEDIA_TYPE = Pattern.compile( | ||
| "[!#$%&'*+.^_`|~0-9A-Za-z-]+/[!#$%&'*+.^_`|~0-9A-Za-z-]+"); | ||
|
|
||
| private final String url; | ||
| private final String contentType; | ||
| private final String length; | ||
|
|
||
| private EnclosureMetadata(String url, String contentType, String length) { | ||
| this.url = url; | ||
| this.contentType = contentType; | ||
| this.length = length; | ||
| } | ||
|
|
||
| public static EnclosureMetadata of(String url, String contentType, String length) { | ||
| String normalizedUrl = normalize(url); | ||
| String normalizedType = normalizeContentType(contentType); | ||
| String normalizedLength = normalize(length); | ||
|
|
||
| if (!isHttpUri(normalizedUrl)) { | ||
| throw new ValidationException(Field.URL, | ||
| "Enclosure URL must be an absolute HTTP or HTTPS URL"); | ||
| } | ||
| if (!MEDIA_TYPE.matcher(normalizedType).matches()) { | ||
| throw new ValidationException(Field.TYPE, | ||
| "Enclosure type must be a valid media type"); | ||
| } | ||
|
|
||
| final long byteLength; | ||
| try { | ||
| byteLength = Long.parseLong(normalizedLength); | ||
| } catch (NumberFormatException e) { | ||
| throw new ValidationException(Field.LENGTH, | ||
| "Enclosure length must be a non-negative integer", e); | ||
| } | ||
| if (byteLength < 0) { | ||
| throw new ValidationException(Field.LENGTH, | ||
| "Enclosure length must be a non-negative integer"); | ||
| } | ||
|
|
||
| return new EnclosureMetadata( | ||
| normalizedUrl, normalizedType, Long.toString(byteLength)); | ||
| } | ||
|
|
||
| private static String normalize(String value) { | ||
| return value == null ? "" : value.trim(); | ||
| } | ||
|
|
||
| private static String normalizeContentType(String value) { | ||
| String type = normalize(value); | ||
| int parameter = type.indexOf(';'); | ||
| if (parameter >= 0) { | ||
| type = type.substring(0, parameter).trim(); | ||
| } | ||
| return type.toLowerCase(Locale.ENGLISH); | ||
| } | ||
|
|
||
| private static boolean isHttpUri(String value) { | ||
| try { | ||
| URI uri = new URI(value); | ||
| String scheme = uri.getScheme(); | ||
| if (!("http".equalsIgnoreCase(scheme) | ||
| || "https".equalsIgnoreCase(scheme))) { | ||
| return false; | ||
| } | ||
| if (uri.getRawAuthority() == null || uri.getRawAuthority().isEmpty()) { | ||
| return false; | ||
| } | ||
| if (uri.getHost() != null && !uri.getHost().isEmpty()) { | ||
| return uri.getPort() <= 65535; | ||
| } | ||
|
|
||
| // URI.getHost() is null for a Unicode authority. Validate its host | ||
| // locally after converting it to ASCII; this performs no DNS or I/O. | ||
| String authority = uri.getRawAuthority(); | ||
| int userInfo = authority.lastIndexOf('@'); | ||
| String hostAndPort = userInfo >= 0 | ||
| ? authority.substring(userInfo + 1) : authority; | ||
| int colon = hostAndPort.lastIndexOf(':'); | ||
| String host = colon >= 0 ? hostAndPort.substring(0, colon) : hostAndPort; | ||
| if (colon >= 0) { | ||
| int port = Integer.parseInt(hostAndPort.substring(colon + 1)); | ||
| if (port > 65535) { | ||
| return false; | ||
| } | ||
| } | ||
| return !IDN.toASCII(host).isEmpty(); | ||
| } catch (IllegalArgumentException | URISyntaxException invalid) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public enum Field { | ||
| URL, TYPE, LENGTH | ||
| } | ||
|
|
||
| public static final class ValidationException extends IllegalArgumentException { | ||
| private final Field field; | ||
|
|
||
| private ValidationException(Field field, String message) { | ||
| super(message); | ||
| this.field = field; | ||
| } | ||
|
|
||
| private ValidationException(Field field, String message, Throwable cause) { | ||
| super(message, cause); | ||
| this.field = field; | ||
| } | ||
|
|
||
| public Field getField() { | ||
| return field; | ||
| } | ||
| } | ||
|
|
||
| public String getUrl() { | ||
| return url; | ||
| } | ||
|
|
||
| public String getContentType() { | ||
| return contentType; | ||
| } | ||
|
|
||
| public String getLength() { | ||
| return length; | ||
| } | ||
| } | ||
62 changes: 0 additions & 62 deletions
62
app/src/main/java/org/apache/roller/weblogger/util/MediacastException.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
Nit: no upper bound;
Long.MAX_VALUEis accepted and the feed advertises an 8 EiB enclosure. The form caps the field at 20 characters, so a sanity ceiling here would match.