Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class EntryBean {
private boolean rightToLeft = false;
private boolean pinnedToMain = false;
private String enclosureURL = null;
private String enclosureType = null;
private String enclosureLength = null;
private String searchDescription = null;
private int commentCount = 0;

Expand Down Expand Up @@ -227,6 +229,22 @@ public String getEnclosureURL() {
public void setEnclosureURL(String enclosureUrl) {
this.enclosureURL = enclosureUrl;
}

public String getEnclosureType() {
return enclosureType;
}

public void setEnclosureType(String enclosureType) {
this.enclosureType = enclosureType;
}

public String getEnclosureLength() {
return enclosureLength;
}

public void setEnclosureLength(String enclosureLength) {
this.enclosureLength = enclosureLength;
}

public String getSearchDescription() {
return searchDescription;
Expand Down Expand Up @@ -390,6 +408,10 @@ public void copyFrom(WeblogEntry entry, Locale locale) {
for (WeblogEntryAttribute attr : attrs) {
if ("att_mediacast_url".equals(attr.getName())) {
setEnclosureURL(attr.getValue());
} else if ("att_mediacast_type".equals(attr.getName())) {
setEnclosureType(attr.getValue());
} else if ("att_mediacast_length".equals(attr.getName())) {
setEnclosureLength(attr.getValue());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@
import org.apache.roller.weblogger.ui.core.plugins.UIPluginManager;
import org.apache.roller.weblogger.ui.core.plugins.WeblogEntryEditor;
import org.apache.roller.weblogger.ui.struts2.util.UIAction;
import org.apache.roller.weblogger.util.cache.CacheManager;
import org.apache.roller.weblogger.util.EnclosureMetadata;
import org.apache.roller.weblogger.util.MailUtil;
import org.apache.roller.weblogger.util.MediacastException;
import org.apache.roller.weblogger.util.MediacastResource;
import org.apache.roller.weblogger.util.MediacastUtil;
import org.apache.roller.weblogger.util.RollerMessages;
import org.apache.roller.weblogger.util.RollerMessages.RollerMessage;
import org.apache.roller.weblogger.util.Trackback;
import org.apache.roller.weblogger.util.TrackbackNotAllowedException;
import org.apache.roller.weblogger.util.cache.CacheManager;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.interceptor.validation.SkipValidation;

Expand Down Expand Up @@ -190,8 +188,13 @@ public String publish() {
*
* @return String The result of the action.
*/
private String save() {
String save() {
if (!hasActionErrors()) {
EnclosureMetadata enclosure = validateEnclosure();
if (hasActionErrors()) {
return failedSave();
}

try {
WeblogEntryManager weblogEntryManager = WebloggerFactory.getWeblogger()
.getWeblogEntryManager();
Expand Down Expand Up @@ -223,24 +226,13 @@ private String save() {
weblogEntry.setPinnedToMain(getBean().getPinnedToMain());
}

if (!StringUtils.isEmpty(getBean().getEnclosureURL())) {
try {
// Fetch MediaCast resource
log.debug("Checking MediaCast attributes");
MediacastResource mediacast = MediacastUtil
.lookupResource(getBean().getEnclosureURL());

// set mediacast attributes
weblogEntry.putEntryAttribute("att_mediacast_url",
mediacast.getUrl());
weblogEntry.putEntryAttribute("att_mediacast_type",
mediacast.getContentType());
weblogEntry.putEntryAttribute("att_mediacast_length", ""
+ mediacast.getLength());

} catch (MediacastException ex) {
addMessage(getText(ex.getErrorKey()));
}
if (enclosure != null) {
weblogEntry.putEntryAttribute("att_mediacast_url",
enclosure.getUrl());
weblogEntry.putEntryAttribute("att_mediacast_type",
enclosure.getContentType());
weblogEntry.putEntryAttribute("att_mediacast_length",
enclosure.getLength());
} else if ("entryEdit".equals(actionName)) {
try {
// if MediaCast string is empty, clean out MediaCast
Expand Down Expand Up @@ -304,8 +296,56 @@ private String save() {
addError("generic.error.check.logs");
}
}
return failedSave();
}

EnclosureMetadata validateEnclosure() {
if (StringUtils.isEmpty(getBean().getEnclosureURL())) {
return null;
}
try {
return EnclosureMetadata.of(
getBean().getEnclosureURL(),
getBean().getEnclosureType(),
getBean().getEnclosureLength());
} catch (EnclosureMetadata.ValidationException invalid) {
if (submittedEnclosureMatchesStored()) {
getBean().setEnclosureURL(null);
getBean().setEnclosureType(null);
getBean().setEnclosureLength(null);
addMessage("weblogEdit.enclosureMetadataRemoved");
} else {
switch (invalid.getField()) {
case URL:
addError("weblogEdit.enclosureURLInvalid");
break;
case TYPE:
addError("weblogEdit.enclosureTypeInvalid");
break;
case LENGTH:
addError("weblogEdit.enclosureLengthInvalid");
break;
default:
throw invalid;
}
}
return null;
}
}

private boolean submittedEnclosureMatchesStored() {
return "entryEdit".equals(actionName) && getEntry() != null
&& StringUtils.equals(getBean().getEnclosureURL(),
getEntry().findEntryAttribute("att_mediacast_url"))
&& StringUtils.equals(getBean().getEnclosureType(),
getEntry().findEntryAttribute("att_mediacast_type"))
&& StringUtils.equals(getBean().getEnclosureLength(),
getEntry().findEntryAttribute("att_mediacast_length"));
}

private String failedSave() {
if ("entryAdd".equals(actionName)) {
// if here on entryAdd, nothing saved, so reset status to null (unsaved)
// If here on entryAdd, nothing saved, so reset status to null (unsaved).
getBean().setStatus(null);
}
return INPUT;
Expand Down
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");
}

Copy link
Copy Markdown
Contributor

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_VALUE is accepted and the feed advertises an 8 EiB enclosure. The form caps the field at 20 characters, so a sanity ceiling here would match.


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;
}
}

This file was deleted.

Loading
Loading