diff --git a/engine/src/main/java/org/archive/crawler/restlet/BaseResource.java b/engine/src/main/java/org/archive/crawler/restlet/BaseResource.java new file mode 100644 index 00000000..846df9e2 --- /dev/null +++ b/engine/src/main/java/org/archive/crawler/restlet/BaseResource.java @@ -0,0 +1,56 @@ +/* + * This file is part of the Heritrix web crawler (crawler.archive.org). + * + * Licensed to the Internet Archive (IA) by one or more individual + * contributors. + * + * The IA 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.archive.crawler.restlet; + +import org.restlet.Context; +import org.restlet.data.MediaType; +import org.restlet.data.Preference; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.resource.Resource; +import org.restlet.resource.Variant; + +/** + * Abstract {@code Resource} with common shared functionality. + * + * @contributor nlevitt + */ +public abstract class BaseResource extends Resource { + + public BaseResource(Context ctx, Request req, Response res) { + super(ctx, req, res); + } + + /** + * If client can accept text/html, always prefer it. WebKit-based browsers + * claim to want application/xml, but we don't want to give it to them. See + * {@link https://webarchive.jira.com/browse/HER-1603} + */ + public Variant getPreferredVariant() { + for (Preference mediaTypePreference: getRequest().getClientInfo().getAcceptedMediaTypes()) { + if (mediaTypePreference.getMetadata().equals(MediaType.TEXT_HTML)) { + mediaTypePreference.setQuality(Float.MAX_VALUE); + break; + } + } + + return super.getPreferredVariant(); + } +} diff --git a/engine/src/main/java/org/archive/crawler/restlet/EngineResource.java b/engine/src/main/java/org/archive/crawler/restlet/EngineResource.java index 6bffb88f..1a4945af 100644 --- a/engine/src/main/java/org/archive/crawler/restlet/EngineResource.java +++ b/engine/src/main/java/org/archive/crawler/restlet/EngineResource.java @@ -44,7 +44,6 @@ import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; import org.restlet.resource.Representation; -import org.restlet.resource.Resource; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; import org.restlet.resource.WriterRepresentation; @@ -56,7 +55,7 @@ import org.xml.sax.SAXException; * * @contributor gojomo */ -public class EngineResource extends Resource { +public class EngineResource extends BaseResource { public EngineResource(Context ctx, Request req, Response res) { super(ctx, req, res); diff --git a/engine/src/main/java/org/archive/crawler/restlet/JobResource.java b/engine/src/main/java/org/archive/crawler/restlet/JobResource.java index 92fc43f7..02c397e6 100644 --- a/engine/src/main/java/org/archive/crawler/restlet/JobResource.java +++ b/engine/src/main/java/org/archive/crawler/restlet/JobResource.java @@ -57,7 +57,6 @@ import org.restlet.data.Request; import org.restlet.data.Response; import org.restlet.data.Status; import org.restlet.resource.Representation; -import org.restlet.resource.Resource; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; import org.restlet.resource.WriterRepresentation; @@ -69,7 +68,7 @@ import org.xml.sax.SAXException; * * @contributor gojomo */ -public class JobResource extends Resource { +public class JobResource extends BaseResource { public static final IOFileFilter EDIT_FILTER = FileUtils.getRegexFileFilter(".*\\.((c?xml)|(txt))$");