Apache Nutch is an extensible and scalable web crawler
Fix for NUTCH-2027 seed list REST endpoint for Nutch 1.10 contributed by Asitang Mishra <[email protected]> this closes #28.
Chris Mattmann8397611b49de
diff --git a/CHANGES.txt b/CHANGES.txtindex 0e24bc977..e2d35c5cb 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogNutch Current Development 1.11-SNAPSHOT+* NUTCH-2027 seed list REST endpoint for Nutch 1.10 (Asitang Mishra via mattmann)+* NUTCH-2031 Create Admin End point for Nutch 1.x REST service (Sujen Shah via mattmann)* NUTCH-2015 Make FetchNodeDb optional (off by default) if NutchServer is not used (Sujen Shah via mattmann)diff --git a/src/java/org/apache/nutch/service/NutchServer.java b/src/java/org/apache/nutch/service/NutchServer.javaindex 14f84d45b..9f7b4fb77 100644--- a/src/java/org/apache/nutch/service/NutchServer.java+++ b/src/java/org/apache/nutch/service/NutchServer.java@@ -49,6 +49,7 @@ import org.apache.nutch.service.resources.AdminResource;import org.apache.nutch.service.resources.ConfigResource;import org.apache.nutch.service.resources.DbResource;import org.apache.nutch.service.resources.JobResource;+import org.apache.nutch.service.resources.SeedResource;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -132,6 +133,7 @@ public class NutchServer {resources.add(ConfigResource.class);resources.add(DbResource.class);resources.add(AdminResource.class);+ resources.add(SeedResource.class);return resources;}diff --git a/src/java/org/apache/nutch/service/model/request/SeedList.java b/src/java/org/apache/nutch/service/model/request/SeedList.javanew file mode 100644index 000000000..f4ef824d4--- /dev/null+++ b/src/java/org/apache/nutch/service/model/request/SeedList.java@@ -0,0 +1,55 @@+/**+ * 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 filediff --git a/src/java/org/apache/nutch/service/resources/SeedResource.java b/src/java/org/apache/nutch/service/resources/SeedResource.javanew file mode 100644index 000000000..1fe75e773--- /dev/null+++ b/src/java/org/apache/nutch/service/resources/SeedResource.java@@ -0,0 +1,115 @@+/**+ * 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.resources;++import static javax.ws.rs.core.Response.status;++import java.io.BufferedWriter;+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;+import javax.ws.rs.POST;+import javax.ws.rs.Path;+import javax.ws.rs.WebApplicationException;+import javax.ws.rs.core.MediaType;+import javax.ws.rs.core.Response;+import javax.ws.rs.core.Response.Status;++import org.apache.commons.collections.CollectionUtils;+import org.apache.nutch.service.model.request.SeedList;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;+++/**+ * used to create a seedlist (for the Nutch server)+ * called seed.txt in the directory/folder provided by the SeedList object+ *+ *+ */++@Path(value = "/seed")+public class SeedResource extends AbstractResource {+ private static final Logger log = LoggerFactory+ .getLogger(SeedResource.class);++ @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);+ }+ }++ return true;+ }++ private void writeUrl(BufferedWriter writer, String seedUrl) {+ try {+ writer.write(seedUrl);+ 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 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 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