Author: mattmann
Date: Fri Jun  5 03:05:59 2015
New Revision: 1683657

URL: http://svn.apache.org/r1683657
Log:
Fix for NUTCH-2027 seed list REST endpoint for Nutch 1.10 contributed by 
Asitang Mishra <[email protected]> this closes #28.

Added:
    nutch/trunk/src/java/org/apache/nutch/service/model/request/SeedList.java
    nutch/trunk/src/java/org/apache/nutch/service/resources/SeedResource.java
Modified:
    nutch/trunk/CHANGES.txt
    nutch/trunk/src/java/org/apache/nutch/service/NutchServer.java

Modified: nutch/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/nutch/trunk/CHANGES.txt?rev=1683657&r1=1683656&r2=1683657&view=diff
==============================================================================
--- nutch/trunk/CHANGES.txt (original)
+++ nutch/trunk/CHANGES.txt Fri Jun  5 03:05:59 2015
@@ -2,6 +2,8 @@ Nutch Change Log
   
 Nutch 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)

Modified: nutch/trunk/src/java/org/apache/nutch/service/NutchServer.java
URL: 
http://svn.apache.org/viewvc/nutch/trunk/src/java/org/apache/nutch/service/NutchServer.java?rev=1683657&r1=1683656&r2=1683657&view=diff
==============================================================================
--- nutch/trunk/src/java/org/apache/nutch/service/NutchServer.java (original)
+++ nutch/trunk/src/java/org/apache/nutch/service/NutchServer.java Fri Jun  5 
03:05:59 2015
@@ -49,6 +49,7 @@ import org.apache.nutch.service.resource
 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;
   }
 

Added: nutch/trunk/src/java/org/apache/nutch/service/model/request/SeedList.java
URL: 
http://svn.apache.org/viewvc/nutch/trunk/src/java/org/apache/nutch/service/model/request/SeedList.java?rev=1683657&view=auto
==============================================================================
--- nutch/trunk/src/java/org/apache/nutch/service/model/request/SeedList.java 
(added)
+++ nutch/trunk/src/java/org/apache/nutch/service/model/request/SeedList.java 
Fri Jun  5 03:05:59 2015
@@ -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 file

Added: nutch/trunk/src/java/org/apache/nutch/service/resources/SeedResource.java
URL: 
http://svn.apache.org/viewvc/nutch/trunk/src/java/org/apache/nutch/service/resources/SeedResource.java?rev=1683657&view=auto
==============================================================================
--- nutch/trunk/src/java/org/apache/nutch/service/resources/SeedResource.java 
(added)
+++ nutch/trunk/src/java/org/apache/nutch/service/resources/SeedResource.java 
Fri Jun  5 03:05:59 2015
@@ -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


Reply via email to