Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

rudimentary bookmark import from firefox bookmarks export file.

  • Loading branch information...
commit 3b871140f13535cde35684e7599771df8d1ffa23 1 parent 9bc9c90
anthonychaves authored
BIN  lib/commons-fileupload-1.2.1.jar
Binary file not shown
BIN  lib/commons-io-1.4.jar
Binary file not shown
BIN  lib/htmlcleaner2_0.jar
Binary file not shown
2  src/META-INF/notes.txt
@@ -55,6 +55,8 @@ Next steps: Apache/AJP
55 55 lin.ke needs to be a white-label service - the built in url shortener
56 56 iphone app for new content from lin.ke
57 57 how do we integrate with it as a service and as an open source package?
  58 + How do I deal with different versions of files with git? mac and htpc have different versions of build.properties
  59 +
58 60
59 61 Done 2010-01-10-ish - Login from cookie in servlet filter
60 62 Done 2010-01-17 - Page title, bookmark tags
4 src/WEB-INF/bookmarks-servlet.xml
@@ -38,6 +38,10 @@
38 38 <!-- component-scan creates implicit AutowiredBeanPostProcessor, we don't need PersistenceAnnotationPostProcessor -->
39 39 <context:component-scan base-package="net.anthonychaves.bookmarks"/>
40 40
  41 + <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  42 + <property name="maxUploadSize" value="100000"/>
  43 + </bean>
  44 +
41 45 <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
42 46 <property name="mediaTypes">
43 47 <map>
8 src/WEB-INF/jsp/user.jsp
@@ -36,6 +36,14 @@ You can generate an API token. Your current API token is: ${user.apiKey}
36 36 <a href="javascript:(function(){targetUrl='http://${pageContext.request.serverName}:8080/bookmarks/b/bookmarks/new?url='+encodeURI(document.location)+'&title='+encodeURI(document.title);window.open(targetUrl,'QuickBookmark','width=400,height=400,location=yes,links=no,toolbar=no');})()">Quick bookmark</a>
37 37 </p>
38 38
  39 +<p>
  40 + You can upload a bookmark file.<br/>
  41 + <form method="post" action="/bookmarks/b/bookmarksFile.json" enctype="multipart/form-data">
  42 + <input type="file" name="file"/>
  43 + <input type="submit"/>
  44 + </form>
  45 +</p>
  46 +
39 47 <jsp:include page="/b/bookmarks/new"/>
40 48 <br/>
41 49 <jsp:include page="/b/bookmarks"/>
17 src/net/anthonychaves/bookmarks/service/UserService.java
@@ -80,6 +80,23 @@ public User addBookmark(User user, Bookmark bookmark) {
80 80 return u;
81 81 }
82 82
  83 + public User addBookmarks(User user, List<Bookmark> bookmarks) {
  84 + EntityManager em = emf.createEntityManager();
  85 +
  86 + em.getTransaction().begin();
  87 + User u = findUser(user.getEmailAddress());
  88 +
  89 + for (Bookmark bookmark : bookmarks) {
  90 + bookmark.setUser(u);
  91 + em.persist(bookmark);
  92 + u.getBookmarks().add(bookmark);
  93 + }
  94 +
  95 + em.getTransaction().commit();
  96 +
  97 + return u;
  98 + }
  99 +
83 100 public void deleteBookmark(User user, String bookmarkId) {
84 101 EntityManager em = emf.createEntityManager();
85 102 em.getTransaction().begin();
74 src/net/anthonychaves/bookmarks/web/BookmarkFileUploadController.java
... ... @@ -0,0 +1,74 @@
  1 +/**
  2 + Copyright 2010 Anthony Chaves
  3 +
  4 + This file is part of Bookmarks.
  5 +
  6 + Bookmarks is free software: you can redistribute it and/or modify
  7 + it under the terms of the GNU General Public License as published by
  8 + the Free Software Foundation, either version 3 of the License, or
  9 + (at your option) any later version.
  10 +
  11 + Bookmarks is distributed in the hope that it will be useful,
  12 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + GNU General Public License for more details.
  15 +
  16 + You should have received a copy of the GNU General Public License
  17 + along with Bookmarks. If not, see <http://www.gnu.org/licenses/>.
  18 +*/
  19 +
  20 +package net.anthonychaves.bookmarks.web;
  21 +
  22 +import org.springframework.web.bind.annotation.*;
  23 +import org.springframework.stereotype.*;
  24 +import org.springframework.ui.*;
  25 +import org.springframework.beans.factory.annotation.*;
  26 +import org.springframework.web.multipart.*;
  27 +
  28 +import java.io.*;
  29 +import java.util.*;
  30 +
  31 +import javax.persistence.*;
  32 +import javax.servlet.http.*;
  33 +
  34 +import net.anthonychaves.bookmarks.models.*;
  35 +import net.anthonychaves.bookmarks.service.*;
  36 +
  37 +import org.htmlcleaner.*;
  38 +
  39 +@Controller
  40 +@RequestMapping("/bookmarksFile")
  41 +public class BookmarkFileUploadController {
  42 +
  43 + @Autowired
  44 + UserService userService;
  45 +
  46 + @RequestMapping(method=RequestMethod.POST)
  47 + public String uploadBookmarkFile(@RequestParam(value="file") MultipartFile file,
  48 + HttpSession session, ModelMap model) throws IOException {
  49 +
  50 + HtmlCleaner cleaner = new HtmlCleaner();
  51 + TagNode root = cleaner.clean(new String(file.getBytes()));
  52 + List<TagNode> nodes = (List<TagNode>) root.getElementListHavingAttribute("href", true);
  53 +
  54 + List<Bookmark> bookmarks = new ArrayList<Bookmark>();
  55 + for (TagNode node : nodes) {
  56 + Bookmark bookmark = new Bookmark();
  57 + bookmark.setTitle(node.getText().toString());
  58 + bookmark.setUrl(node.getAttributeByName("href"));
  59 + bookmarks.add(bookmark);
  60 + }
  61 +
  62 + User user = (User) session.getAttribute("user");
  63 + user = userService.addBookmarks(user, bookmarks);
  64 + session.setAttribute("user", user);
  65 +
  66 + model.addAttribute("i think we succeeded");
  67 +
  68 + return "redirect:/b/user";
  69 + }
  70 +
  71 + public void setUserService(UserService userService) {
  72 + this.userService = userService;
  73 + }
  74 +}

0 comments on commit 3b87114

Please sign in to comment.
Something went wrong with that request. Please try again.