Apache Nutch is an extensible and scalable web crawler
Fix for NUTCH-2090: Refactor Seed Resource in REST API contributed by Sujen Shah.
Chris Mattmann4b93ace638fa
diff --git a/CHANGES.txt b/CHANGES.txtindex 49558de29..be89d638e 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,9 @@ Nutch Change LogNutch Current Development 1.11-SNAPSHOT+* NUTCH-2090 Refactor Seed Resource in REST API (Sujen Shah+ via mattmann)+* NUTCH-2088 Add URL Processing Check to Interactive SeleniumHandlers (Michael Joyce via mattmann)diff --git a/src/java/org/apache/nutch/service/model/request/SeedList.java b/src/java/org/apache/nutch/service/model/request/SeedList.javaindex f4ef824d4..91dd1bbc8 100644--- a/src/java/org/apache/nutch/service/model/request/SeedList.java+++ b/src/java/org/apache/nutch/service/model/request/SeedList.java@@ -1,55 +1,79 @@-/**+/******************************************************************************** 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.nutch.service.model.request;-import java.util.ArrayList;--/**- * This is the seedlist object containing a list of seeds to be used to create a seedlist in the provided directory.- *- *- */-public class SeedList{-- private String directory;- private ArrayList<String> seedUrls;---- public ArrayList<String> getSeedUrls() {- return seedUrls;- }-- public void setSeedUrls(ArrayList<String> seedUrls) {- this.seedUrls = seedUrls;- }--- public void setDirectory(String directory) {- this.directory = directory;- }-- public String getDirectory() {- return directory;- }-------}\ No newline at end of file+import java.io.Serializable;+import java.util.Collection;++public class SeedList implements Serializable {++ private Long id;++ private String name;++ private Collection<SeedUrl> seedUrls;++ public Long getId() {+ return id;+ }++ public void setId(Long id) {+ this.id = id;+ }++ public Collection<SeedUrl> getSeedUrls() {+ return seedUrls;+ }++ public void setSeedUrls(Collection<SeedUrl> seedUrls) {+ this.seedUrls = seedUrls;+ }++ public String getName() {+ return name;+ }++ public void setName(String name) {+ this.name = name;+ }++ @Override+ public int hashCode() {+ final int prime = 31;+ int result = 1;+ result = prime * result + ((id == null) ? 0 : id.hashCode());+ return result;+ }++ @Override+ public boolean equals(Object obj) {+ if (this == obj)+ return true;+ if (obj == null)+ return false;+ if (getClass() != obj.getClass())+ return false;+ SeedList other = (SeedList) obj;+ if (id == null) {+ if (other.id != null)+ return false;+ } else if (!id.equals(other.id))+ return false;+ return true;+ }++}diff --git a/src/java/org/apache/nutch/service/model/request/SeedUrl.java b/src/java/org/apache/nutch/service/model/request/SeedUrl.javanew file mode 100644index 000000000..befda5c92--- /dev/null+++ b/src/java/org/apache/nutch/service/model/request/SeedUrl.java@@ -0,0 +1,81 @@+/*******************************************************************************+ * 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.nutch.service.model.request;++import java.io.Serializable;++public class SeedUrl implements Serializable {++ private Long id;++ private SeedList seedList;++ private String url;++ public SeedUrl(String url) {+ this.url = url;+ }++ public Long getId() {+ return id;+ }++ public void setId(Long id) {+ this.id = id;+ }++ public String getUrl() {+ return url;+ }++ public void setUrl(String url) {+ this.url = url;+ }++ public SeedList getSeedList() {+ return seedList;+ }++ public void setSeedList(SeedList seedList) {+ this.seedList = seedList;+ }++ @Override+ public int hashCode() {+ final int prime = 31;+ int result = 1;+ result = prime * result + ((id == null) ? 0 : id.hashCode());+ return result;+ }++ @Override+ public boolean equals(Object obj) {+ if (this == obj)+ return true;+ if (obj == null)+ return false;+ if (getClass() != obj.getClass())+ return false;+ SeedUrl other = (SeedUrl) obj;+ if (id == null) {+ if (other.id != null)+ return false;+ } else if (!id.equals(other.id))+ return false;+ return true;+ }+}diff --git a/src/java/org/apache/nutch/service/resources/SeedResource.java b/src/java/org/apache/nutch/service/resources/SeedResource.javaindex 1fe75e773..8154208b4 100644--- a/src/java/org/apache/nutch/service/resources/SeedResource.java+++ b/src/java/org/apache/nutch/service/resources/SeedResource.java@@ -23,8 +23,6 @@ import java.io.File;import java.io.FileNotFoundException;import java.io.FileWriter;import java.io.IOException;-import java.nio.file.Files;-import java.nio.file.Paths;import java.util.Collection;import javax.ws.rs.Consumes;@@ -37,79 +35,75 @@ import javax.ws.rs.core.Response.Status;import org.apache.commons.collections.CollectionUtils;import org.apache.nutch.service.model.request.SeedList;+import org.apache.nutch.service.model.request.SeedUrl;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import com.google.common.io.Files;-/**- * used to create a seedlist (for the Nutch server)- * called seed.txt in the directory/folder provided by the SeedList object- *- *- */--@Path(value = "/seed")+@Path("/seed")public class SeedResource extends AbstractResource {- private static final Logger log = LoggerFactory- .getLogger(SeedResource.class);+ private static final Logger log = LoggerFactory+ .getLogger(AdminResource.class);++ @POST+ @Path("/create")+ @Consumes(MediaType.APPLICATION_JSON)+ /**+ * Method creates seed list file and returns temorary directory path+ * @param seedList+ * @return+ */+ public String createSeedFile(SeedList seedList) {+ if (seedList == null) {+ throw new WebApplicationException(Response.status(Status.BAD_REQUEST)+ .entity("Seed list cannot be empty!").build());+ }+ File seedFile = createSeedFile();+ BufferedWriter writer = getWriter(seedFile);- @POST- @Path(value = "/create")- @Consumes(MediaType.APPLICATION_JSON)- public boolean create(SeedList seedList) {- if (seedList == null) {- throw new WebApplicationException(Response.status(Status.BAD_REQUEST)- .entity("Seed list cannot be empty!").build());- }- File seedFile = createSeedFile(seedList.getDirectory());- BufferedWriter writer = getWriter(seedFile);- Collection<String> seedUrls = seedList.getSeedUrls();- if (CollectionUtils.isNotEmpty(seedUrls)) {- for (String seedUrl : seedUrls) {- writeUrl(writer, seedUrl);- }- }+ Collection<SeedUrl> seedUrls = seedList.getSeedUrls();+ if (CollectionUtils.isNotEmpty(seedUrls)) {+ for (SeedUrl seedUrl : seedUrls) {+ writeUrl(writer, seedUrl);+ }+ }- return true;- }+ return seedFile.getParent();+ }- private void writeUrl(BufferedWriter writer, String seedUrl) {- try {- writer.write(seedUrl);- writer.newLine();- writer.flush();- } catch (IOException e) {- throw handleException(e);- }- }+ private void writeUrl(BufferedWriter writer, SeedUrl seedUrl) {+ try {+ writer.write(seedUrl.getUrl());+ writer.newLine();+ writer.flush();+ } catch (IOException e) {+ throw handleException(e);+ }+ }- private BufferedWriter getWriter(File seedFile) {- try {- return new BufferedWriter(new FileWriter(seedFile));- } catch (FileNotFoundException e) {- throw handleException(e);- } catch (IOException e) {- throw handleException(e);- }- }+ private BufferedWriter getWriter(File seedFile) {+ try {+ return new BufferedWriter(new FileWriter(seedFile));+ } catch (FileNotFoundException e) {+ throw handleException(e);+ } catch (IOException e) {+ throw handleException(e);+ }+ }- private File createSeedFile(String directory) {- try {- java.nio.file.Path path=Paths.get(directory);- if (!Files.exists(path)) {- new File(directory).mkdir();- }- File targetFile = new File(directory+"/seed.txt");- return targetFile;- } catch (Exception e) {- throw handleException(e);- }- }+ private File createSeedFile() {+ try {+ return File.createTempFile("seed", ".txt", Files.createTempDir());+ } catch (IOException e) {+ throw handleException(e);+ }+ }- private RuntimeException handleException(Exception e) {- log.error("Cannot create seed file!", e);- return new WebApplicationException(status(Status.INTERNAL_SERVER_ERROR)- .entity("Cannot create seed file!").build());- }+ private RuntimeException handleException(Exception e) {+ log.error("Cannot create seed file!", e);+ return new WebApplicationException(status(Status.INTERNAL_SERVER_ERROR)+ .entity("Cannot create seed file!").build());+ }-}\ No newline at end of file+}