Author: mattmann
Date: Mon Sep  7 03:00:27 2015
New Revision: 1701539

URL: http://svn.apache.org/r1701539
Log:
Fix for NUTCH-2090: Refactor Seed Resource in REST API contributed by Sujen 
Shah.

Added:
    nutch/trunk/src/java/org/apache/nutch/service/model/request/SeedUrl.java
Modified:
    nutch/trunk/CHANGES.txt
    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
URL: 
http://svn.apache.org/viewvc/nutch/trunk/CHANGES.txt?rev=1701539&r1=1701538&r2=1701539&view=diff
==============================================================================
--- nutch/trunk/CHANGES.txt (original)
+++ nutch/trunk/CHANGES.txt Mon Sep  7 03:00:27 2015
@@ -2,6 +2,9 @@ Nutch Change Log
   
 Nutch 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 Selenium 
   Handlers (Michael Joyce via mattmann)
 

Modified: 
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=1701539&r1=1701538&r2=1701539&view=diff
==============================================================================
--- nutch/trunk/src/java/org/apache/nutch/service/model/request/SeedList.java 
(original)
+++ nutch/trunk/src/java/org/apache/nutch/service/model/request/SeedList.java 
Mon Sep  7 03:00:27 2015
@@ -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;
-       }
-
+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;
+  }
 
-}
\ No newline at end of file
+}

Added: nutch/trunk/src/java/org/apache/nutch/service/model/request/SeedUrl.java
URL: 
http://svn.apache.org/viewvc/nutch/trunk/src/java/org/apache/nutch/service/model/request/SeedUrl.java?rev=1701539&view=auto
==============================================================================
--- nutch/trunk/src/java/org/apache/nutch/service/model/request/SeedUrl.java 
(added)
+++ nutch/trunk/src/java/org/apache/nutch/service/model/request/SeedUrl.java 
Mon Sep  7 03:00:27 2015
@@ -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;
+  }
+}

Modified: 
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=1701539&r1=1701538&r2=1701539&view=diff
==============================================================================
--- nutch/trunk/src/java/org/apache/nutch/service/resources/SeedResource.java 
(original)
+++ nutch/trunk/src/java/org/apache/nutch/service/resources/SeedResource.java 
Mon Sep  7 03:00:27 2015
@@ -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(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());
-       }
+  @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);
+
+    Collection<SeedUrl> seedUrls = seedList.getSeedUrls();
+    if (CollectionUtils.isNotEmpty(seedUrls)) {
+      for (SeedUrl seedUrl : seedUrls) {
+        writeUrl(writer, seedUrl);
+      }
+    }
+
+    return seedFile.getParent();
+  }
+
+  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 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());
+  }
 
-}
\ No newline at end of file
+}


Reply via email to