Changeset 9919 for extensions/jiwigo-ws-api/src/main
- Timestamp:
- Mar 29, 2011, 8:21:25 PM (14 years ago)
- Location:
- extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo
- Files:
-
- 3 added
- 1 deleted
- 10 edited
- 9 copied
Legend:
- Unmodified
- Added
- Removed
-
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/CategoryDao.java
r9902 r9919 2 2 3 3 import java.io.IOException; 4 import java.util.ArrayList;5 4 import java.util.List; 6 5 7 import org.w3c.dom.Document; 8 import org.w3c.dom.Element; 9 import org.w3c.dom.Node; 10 import org.w3c.dom.NodeList; 6 import fr.mael.jiwigo.om.Category; 7 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 11 8 12 import fr.mael.jiwigo.om.Category; 13 import fr.mael.jiwigo.transverse.enumeration.CategoryEnum; 14 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; 15 import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; 16 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 17 import fr.mael.jiwigo.transverse.session.SessionManager; 18 import fr.mael.jiwigo.transverse.util.Tools; 19 20 /* 21 * jiwigo-ws-api Piwigo webservice access Api 22 * Copyright (c) 2010-2011 Mael mael@le-guevel.com 23 * All Rights Reserved 24 * 25 * This library is free software. It comes without any warranty, to 26 * the extent permitted by applicable law. You can redistribute it 27 * and/or modify it under the terms of the Do What The Fuck You Want 28 * To Public License, Version 2, as published by Sam Hocevar. See 29 * http://sam.zoy.org/wtfpl/COPYING for more details. 30 */ 31 /** 32 * Dao for the categories 33 * @author mael 34 * 35 */ 36 public class CategoryDao { 37 /** 38 * Logger 39 */ 40 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 41 .getLog(CategoryDao.class); 42 /** 43 * Instance to use a singleton 44 */ 45 private static CategoryDao instance; 46 47 private SessionManager sessionManager; 48 49 /** 50 * Private constructor to use a singleton 51 */ 52 private CategoryDao(SessionManager sessionManager) { 53 this.sessionManager = sessionManager; 54 } 55 56 /** 57 * @return the instance 58 */ 59 public static CategoryDao getInstance(SessionManager sessionManager) { 60 if (instance == null) { 61 instance = new CategoryDao(sessionManager); 62 } 63 return instance; 64 } 9 public interface CategoryDao { 65 10 66 11 /** … … 71 16 * @throws ProxyAuthenticationException 72 17 */ 73 public List<Category> list(boolean recursive) throws IOException, ProxyAuthenticationException { 74 Document doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_CATEGORIES.getLabel(), "recursive", 75 String.valueOf(recursive)); 76 Element element = (Element) doc.getDocumentElement().getElementsByTagName("categories").item(0); 77 NodeList nodeList = element.getElementsByTagName("category"); 78 ArrayList<Category> categories = new ArrayList<Category>(); 79 for (int i = 0; i < nodeList.getLength(); i++) { 80 Node catNode = nodeList.item(i); 81 if (catNode.getNodeType() == Node.ELEMENT_NODE) { 82 Element cat = (Element) catNode; 83 Category myCat = new Category(); 84 myCat.setIdentifier(Integer.valueOf(cat.getAttribute(CategoryEnum.ID.getLabel()))); 85 myCat.setUrlCategory(cat.getAttribute(CategoryEnum.URL.getLabel())); 86 myCat.setNbImages(Integer.valueOf(cat.getAttribute(CategoryEnum.NB_IMAGES.getLabel()))); 87 myCat.setNbTotalImages(Integer.valueOf(cat.getAttribute(CategoryEnum.NB_TOTAL_IMAGES.getLabel()))); 88 myCat.setName(Tools.getStringValueDom(cat, CategoryEnum.NAME.getLabel())); 89 String catMeres = Tools.getStringValueDom(cat, CategoryEnum.CAT_MERES.getLabel()); 90 ArrayList<Integer> idCategoriesMeres = new ArrayList<Integer>(); 91 myCat.setIdParentCategoriesString(catMeres); 92 for (String catA : catMeres.split(",")) { 93 if (!catA.equals("")) { 94 idCategoriesMeres.add(Integer.valueOf(catA)); 95 } 96 } 97 myCat.setIdParentCategories(idCategoriesMeres); 98 categories.add(myCat); 99 } 100 } 101 return categories; 102 } 18 public List<Category> list(boolean recursive) throws IOException, ProxyAuthenticationException; 103 19 104 20 /** … … 108 24 * @throws ProxyAuthenticationException 109 25 */ 110 public boolean create(Category category) throws ProxyAuthenticationException { 111 try { 112 if (category.getDirectParent() != null) { 113 return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(), 114 "name", category.getName(), "parent", String.valueOf(category.getDirectParent()))); 115 } else { 116 return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(), 117 "name", category.getName())); 118 } 119 } catch (IOException e) { 120 LOG.error(Tools.getStackTrace(e)); 121 } catch (FileAlreadyExistsException e) { 122 LOG.error(Tools.getStackTrace(e)); 123 } 124 return false; 125 } 126 26 public boolean create(Category category) throws ProxyAuthenticationException; 127 27 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/CommentDao.java
r9902 r9919 2 2 3 3 import java.io.IOException; 4 import java.util.ArrayList;5 4 import java.util.List; 6 5 7 import org.w3c.dom.Document; 8 import org.w3c.dom.Element; 9 import org.w3c.dom.Node; 10 import org.w3c.dom.NodeList; 6 import fr.mael.jiwigo.om.Comment; 7 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 11 8 12 import fr.mael.jiwigo.om.Comment; 13 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; 14 import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; 15 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 16 import fr.mael.jiwigo.transverse.session.SessionManager; 17 import fr.mael.jiwigo.transverse.util.Tools; 18 19 /* 20 * jiwigo-ws-api Piwigo webservice access Api 21 * Copyright (c) 2010-2011 Mael mael@le-guevel.com 22 * All Rights Reserved 23 * 24 * This library is free software. It comes without any warranty, to 25 * the extent permitted by applicable law. You can redistribute it 26 * and/or modify it under the terms of the Do What The Fuck You Want 27 * To Public License, Version 2, as published by Sam Hocevar. See 28 * http://sam.zoy.org/wtfpl/COPYING for more details. 29 */ 30 /** 31 32 * Dao for the comments 33 * @author mael 34 * 35 */ 36 public class CommentDao { 37 /** 38 * Logger 39 */ 40 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 41 .getLog(CommentDao.class); 42 /** 43 * Instance that allows to use a singleton 44 */ 45 private static CommentDao instance; 46 47 private SessionManager sessionManager; 48 49 /** 50 * private constructor, to use a singleton 51 */ 52 private CommentDao(SessionManager sessionManager) { 53 this.sessionManager = sessionManager; 54 } 55 56 /** 57 * @return the singleton 58 */ 59 public static CommentDao getInstance(SessionManager sessionManager) { 60 if (instance == null) { 61 instance = new CommentDao(sessionManager); 62 } 63 return instance; 64 } 65 9 public interface CommentDao { 66 10 /** 67 11 * Listing of the comments for the given image … … 71 15 * @throws ProxyAuthenticationException 72 16 */ 73 public List<Comment> list(Integer idImage) throws IOException, ProxyAuthenticationException { 74 Document doc = (sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id", String 75 .valueOf(idImage), "comments_per_page", "100")); 76 Element elementImage = (Element) doc.getDocumentElement().getElementsByTagName("image").item(0); 77 Element elementComments = (Element) elementImage.getElementsByTagName("comments").item(0); 78 NodeList listComments = elementComments.getElementsByTagName("comment"); 79 ArrayList<Comment> comments = new ArrayList<Comment>(); 80 for (int i = 0; i < listComments.getLength(); i++) { 81 Node nodeCom = listComments.item(i); 82 if (nodeCom.getNodeType() == Node.ELEMENT_NODE) { 83 Element com = (Element) nodeCom; 84 Comment myCom = new Comment(); 85 myCom.setIdentifier(Integer.valueOf(com.getAttribute("id"))); 86 myCom.setDate(com.getAttribute("date")); 87 myCom.setAuthor(Tools.getStringValueDom(com, "author")); 88 myCom.setContent(Tools.getStringValueDom(com, "content")); 89 comments.add(myCom); 90 } 91 } 92 return comments; 93 } 17 public List<Comment> list(Integer idImage) throws IOException, ProxyAuthenticationException; 94 18 95 19 /** … … 100 24 * @throws ProxyAuthenticationException 101 25 */ 102 public String getKey(Integer idImage) throws IOException, ProxyAuthenticationException { 103 Document doc = (sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id", String 104 .valueOf(idImage))); 105 // String key = doc.getRootElement().getChild("image").getChild("comment_post").getAttributeValue("key"); 106 Element elementImage = (Element) doc.getDocumentElement().getElementsByTagName("image").item(0); 107 Element elementCommentPost = (Element) elementImage.getElementsByTagName("comment_post").item(0); 108 return elementCommentPost.getAttribute("key"); 109 } 26 public String getKey(Integer idImage) throws IOException, ProxyAuthenticationException; 110 27 111 28 /** … … 116 33 * @throws ProxyAuthenticationException 117 34 */ 118 public boolean create(Comment commentaire) throws IOException, ProxyAuthenticationException { 119 String key = getKey(commentaire.getImageId()); 120 try { 121 Thread.sleep(2200); 122 } catch (InterruptedException e) { 123 e.printStackTrace(); 124 } 125 try { 126 return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_COMMENTAIRE.getLabel(), 127 "image_id", String.valueOf(commentaire.getImageId()), "author", commentaire.getAuthor(), "content", 128 commentaire.getContent(), "key", key)); 129 } catch (FileAlreadyExistsException e) { 130 LOG.error(Tools.getStackTrace(e)); 131 return false; 132 } 133 134 } 35 public boolean create(Comment commentaire) throws IOException, ProxyAuthenticationException; 135 36 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/ImageDao.java
r9902 r9919 1 1 package fr.mael.jiwigo.dao; 2 2 3 import java.io.File;4 3 import java.io.IOException; 5 4 import java.security.NoSuchAlgorithmException; 6 import java.util.ArrayList;7 import java.util.HashMap;8 5 import java.util.List; 9 6 10 import org.w3c.dom.Document;11 import org.w3c.dom.Element;12 import org.w3c.dom.Node;13 import org.w3c.dom.NodeList;14 15 import sun.misc.BASE64Encoder;16 7 import fr.mael.jiwigo.om.Image; 17 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;18 8 import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; 19 9 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 20 10 import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException; 21 import fr.mael.jiwigo.transverse.session.SessionManager;22 import fr.mael.jiwigo.transverse.util.Tools;23 11 24 /* 25 * jiwigo-ws-api Piwigo webservice access Api 26 * Copyright (c) 2010-2011 Mael mael@le-guevel.com 27 * All Rights Reserved 28 * 29 * This library is free software. It comes without any warranty, to 30 * the extent permitted by applicable law. You can redistribute it 31 * and/or modify it under the terms of the Do What The Fuck You Want 32 * To Public License, Version 2, as published by Sam Hocevar. See 33 * http://sam.zoy.org/wtfpl/COPYING for more details. 34 */ 35 /** 36 37 * Dao of the images 38 * @author mael 39 * 40 */ 41 public class ImageDao { 42 43 /** 44 * Logger 45 */ 46 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 47 .getLog(ImageDao.class); 48 49 /** 50 * Singleton 51 */ 52 private static ImageDao instance; 53 54 /** 55 * cache to avoid downloading image for each access 56 */ 57 private HashMap<Integer, List<Image>> cache; 58 59 /** 60 * 61 */ 62 private Integer firstCatInCache; 63 64 private SessionManager sessionManager; 65 66 private ArrayList<File> filesToSend; 67 68 /** 69 * Private singleton, to use a singleton 70 */ 71 private ImageDao(SessionManager sessionManager) { 72 cache = new HashMap<Integer, List<Image>>(); 73 this.sessionManager = sessionManager; 74 } 75 76 /** 77 * @return le singleton 78 */ 79 public static ImageDao getInstance(SessionManager sessionManager) { 80 if (instance == null) { 81 instance = new ImageDao(sessionManager); 82 } 83 return instance; 84 } 85 12 public interface ImageDao { 86 13 /** 87 14 * Lists all images … … 90 17 * @throws ProxyAuthenticationException 91 18 */ 92 public List<Image> list(boolean refresh) throws IOException, ProxyAuthenticationException { 93 return listByCategory(null, refresh); 94 } 19 public List<Image> list(boolean refresh) throws IOException, ProxyAuthenticationException; 95 20 96 21 /** … … 102 27 */ 103 28 public List<Image> listByCategory(Integer categoryId, boolean refresh) throws IOException, 104 ProxyAuthenticationException { 105 if (refresh || cache.get(categoryId) == null) { 106 Document doc = null; 107 if (categoryId != null) { 108 doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel(), "cat_id", String 109 .valueOf(categoryId)); 110 } else { 111 doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel()); 112 } 113 Element element = (Element) doc.getDocumentElement().getElementsByTagName("images").item(0); 114 List<Image> images = getImagesFromElement(element); 115 cache.remove(categoryId); 116 cache.put(categoryId, images); 117 if (firstCatInCache == null) { 118 firstCatInCache = categoryId; 119 } 120 return images; 121 } else { 122 return cache.get(categoryId); 123 } 124 } 29 ProxyAuthenticationException; 125 30 126 31 /** … … 143 48 //TODO ne pas continuer si une des reponses precedentes est negative 144 49 public boolean create(Image image, Double chunkSize) throws FileAlreadyExistsException, IOException, 145 ProxyAuthenticationException, NoSuchAlgorithmException, WrongChunkSizeException { 146 //thumbnail converted to base64 147 BASE64Encoder base64 = new BASE64Encoder(); 148 149 String thumbnailBase64 = base64.encode(Tools.getBytesFromFile(image.getThumbnail())); 150 //sends the thumbnail and gets the result 151 Document reponseThumb = (sessionManager.executeReturnDocument("pwg.images.addChunk", "data", thumbnailBase64, 152 "type", "thumb", "position", "1", "original_sum", Tools.getMD5Checksum(image.getOriginale() 153 .getAbsolutePath()))); 154 155 //begin feature:0001827 156 int chunk = chunkSize.intValue(); 157 if (chunk == 0) { 158 throw new WrongChunkSizeException("Error : the chunk size cannot be 0"); 159 } 160 filesToSend = Tools.splitFile(image.getOriginale(), chunk); 161 boolean echec = false; 162 for (int i = 0; i < filesToSend.size(); i++) { 163 File fichierAEnvoyer = filesToSend.get(i); 164 String originaleBase64 = base64.encode(Tools.getBytesFromFile(fichierAEnvoyer)); 165 Document reponseOriginale = (sessionManager.executeReturnDocument("pwg.images.addChunk", "data", 166 originaleBase64, "type", "file", "position", String.valueOf(i), "original_sum", Tools 167 .getMD5Checksum(image.getOriginale().getAbsolutePath()))); 168 if (!Tools.checkOk(reponseOriginale)) { 169 echec = true; 170 break; 171 } 172 } 173 //end 174 175 //add the image in the database and get the result of the webservice 176 Document reponseAjout = (sessionManager.executeReturnDocument("pwg.images.add", "file_sum", Tools 177 .getMD5Checksum(image.getOriginale().getAbsolutePath()), "thumbnail_sum", Tools.getMD5Checksum(image 178 .getThumbnail().getCanonicalPath()), "position", "1", "original_sum", Tools.getMD5Checksum(image 179 .getOriginale().getAbsolutePath()), "categories", String.valueOf(image.getIdCategory()), "name", image 180 .getName(), "author", sessionManager.getLogin(), "level", image.getPrivacyLevel())); 181 LOG.debug("Response add : " + Tools.documentToString(reponseAjout)); 182 // System.out.println(Main.sessionManager.executerReturnString("pwg.images.add", "file_sum", Outil 183 // .getMD5Checksum(image.getOriginale().getAbsolutePath()), "thumbnail_sum", Outil.getMD5Checksum(image 184 // .getThumbnail().getCanonicalPath()), "position", "1", "original_sum", Outil.getMD5Checksum(image 185 // .getOriginale().getAbsolutePath()), "categories", String.valueOf(image.getIdCategory()), "name", image 186 // .getName(), "author", Main.sessionManager.getLogin())); 187 // Document reponsePrivacy = null; 188 // if (Outil.checkOk(reponseAjout)) { 189 // reponsePrivacy = Main.sessionManager.executerReturnDocument(MethodsEnum.SET_PRIVACY_LEVEL.getLabel()); 190 // } 191 boolean reussite = true; 192 if (!Tools.checkOk(reponseThumb) || echec || !Tools.checkOk(reponseAjout)) { 193 reussite = false; 194 } 195 deleteTempFiles(); 196 return reussite; 197 198 } 50 ProxyAuthenticationException, NoSuchAlgorithmException, WrongChunkSizeException; 199 51 200 52 /** … … 205 57 * @throws ProxyAuthenticationException 206 58 */ 207 public boolean addTags(Integer imageId, String tagId) throws IOException, ProxyAuthenticationException { 208 Document doc = sessionManager.executeReturnDocument(MethodsEnum.SET_INFO.getLabel(), "image_id", String 209 .valueOf(imageId), "tag_ids", tagId); 210 try { 211 return Tools.checkOk(doc); 212 } catch (FileAlreadyExistsException e) { 213 LOG.error(Tools.getStackTrace(e)); 214 return false; 215 } 216 217 } 218 219 /** 220 * parse an element to find images 221 * @param element the element to parse 222 * @return the list of images 223 */ 224 private List<Image> getImagesFromElement(Element element) { 225 // List<Element> listElement = (List<Element>) element.getChildren("image"); 226 NodeList listImages = element.getElementsByTagName("image"); 227 ArrayList<Image> images = new ArrayList<Image>(); 228 for (int i = 0; i < listImages.getLength(); i++) { 229 Node nodeImage = listImages.item(i); 230 if (nodeImage.getNodeType() == Node.ELEMENT_NODE) { 231 Element im = (Element) nodeImage; 232 Image myImage = new Image(); 233 myImage.setThumbnailUrl(im.getAttribute("tn_url")); 234 myImage.setUrl(im.getAttribute("element_url")); 235 myImage.setWidth(Integer.valueOf(im.getAttribute("width"))); 236 myImage.setHeight(Integer.valueOf(im.getAttribute("height"))); 237 myImage.setFile(im.getAttribute("file")); 238 myImage.setSeen(Integer.valueOf(im.getAttribute("hit"))); 239 myImage.setIdentifier(Integer.valueOf(im.getAttribute("id"))); 240 myImage.setName(Tools.getStringValueDom(im, "name")); 241 Element elementCategories = (Element) im.getElementsByTagName("categories").item(0); 242 Element elementCategory = (Element) elementCategories.getElementsByTagName("category").item(0); 243 myImage.setIdCategory(Integer.valueOf(elementCategory.getAttribute("id"))); 244 if (myImage.getName() == null) { 245 myImage.setName(myImage.getFile()); 246 } 247 images.add(myImage); 248 } 249 } 250 return images; 251 } 59 public boolean addTags(Integer imageId, String tagId) throws IOException, ProxyAuthenticationException; 252 60 253 61 /** … … 258 66 * @throws ProxyAuthenticationException 259 67 */ 260 public List<Image> search(String searchString) throws IOException, ProxyAuthenticationException { 261 Document doc = sessionManager.executeReturnDocument(MethodsEnum.SEARCH.getLabel(), "query", searchString); 262 LOG.debug(doc); 263 Element element = (Element) doc.getDocumentElement().getElementsByTagName("images").item(0); 264 return getImagesFromElement(element); 265 266 } 267 268 private void deleteTempFiles() { 269 File file = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg"); 270 file.delete(); 271 file = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg"); 272 file.delete(); 273 for (File tempCut : filesToSend) { 274 tempCut.delete(); 275 } 276 277 } 68 public List<Image> search(String searchString) throws IOException, ProxyAuthenticationException; 278 69 279 70 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/TagDao.java
r9902 r9919 2 2 3 3 import java.io.IOException; 4 import java.util.ArrayList;5 4 import java.util.List; 6 7 import org.w3c.dom.Document;8 import org.w3c.dom.Element;9 import org.w3c.dom.Node;10 import org.w3c.dom.NodeList;11 5 12 6 import fr.mael.jiwigo.om.Image; 13 7 import fr.mael.jiwigo.om.Tag; 14 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;15 import fr.mael.jiwigo.transverse.enumeration.TagEnum;16 import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;17 8 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 18 import fr.mael.jiwigo.transverse.session.SessionManager;19 import fr.mael.jiwigo.transverse.util.Tools;20 9 21 /* 22 * jiwigo-ws-api Piwigo webservice access Api 23 * Copyright (c) 2010-2011 Mael mael@le-guevel.com 24 * All Rights Reserved 25 * 26 * This library is free software. It comes without any warranty, to 27 * the extent permitted by applicable law. You can redistribute it 28 * and/or modify it under the terms of the Do What The Fuck You Want 29 * To Public License, Version 2, as published by Sam Hocevar. See 30 * http://sam.zoy.org/wtfpl/COPYING for more details. 31 */ 32 /** 33 * Dao of the categories 34 * @author mael 35 * 36 */ 37 public class TagDao { 38 /** 39 * Logger 40 */ 41 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(TagDao.class); 42 /** 43 * Instance, to use a singleton 44 */ 45 private static TagDao instance; 46 47 private SessionManager sessionManager; 48 49 /** 50 * private constructor to use a singleton 51 */ 52 private TagDao(SessionManager sessionManager) { 53 this.sessionManager = sessionManager; 54 } 55 56 /** 57 * @return the instance 58 */ 59 public static TagDao getInstance(SessionManager sessionManager) { 60 if (instance == null) { 61 instance = new TagDao(sessionManager); 62 } 63 return instance; 64 } 65 10 public interface TagDao { 66 11 /** 67 12 * lists the tags … … 70 15 * @throws ProxyAuthenticationException 71 16 */ 72 public List<Tag> list() throws IOException, ProxyAuthenticationException { 73 Document doc = sessionManager.executeReturnDocument(MethodsEnum.TAGS_ADMIN_LIST.getLabel()); 74 // System.out.println(Outil.documentToString(doc)); 75 76 return getTagsFromDocument((Element) doc.getDocumentElement().getElementsByTagName("tags")); 77 78 } 79 80 /** 81 * COnstructs a list of tags from a document 82 * @param doc the document 83 * @return the list of tags 84 */ 85 private List<Tag> getTagsFromDocument(Element element) { 86 NodeList listTags = element.getElementsByTagName("tag"); 87 88 // List<Element> listElement = (List<Element>) element.getChildren("tag"); 89 ArrayList<Tag> tags = new ArrayList<Tag>(); 90 for (int i = 0; i < listTags.getLength(); i++) { 91 Node nodeTag = listTags.item(0); 92 if (nodeTag.getNodeType() == Node.ELEMENT_NODE) { 93 Element tagElement = (Element) nodeTag; 94 Tag tag = new Tag(); 95 tag.setIdentifier(Integer.valueOf(tagElement.getAttribute(TagEnum.ID.getLabel()))); 96 tag.setName(tagElement.getAttribute(TagEnum.NAME.getLabel())); 97 tags.add(tag); 98 } 99 } 100 return tags; 101 102 } 17 public List<Tag> list() throws IOException, ProxyAuthenticationException; 103 18 104 19 /** … … 108 23 * @throws ProxyAuthenticationException 109 24 */ 110 public boolean create(Tag tag) throws ProxyAuthenticationException { 111 try { 112 return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.ADD_TAG.getLabel(), "name", tag 113 .getName())); 114 } catch (IOException e) { 115 LOG.error(Tools.getStackTrace(e)); 116 } catch (FileAlreadyExistsException e) { 117 e.printStackTrace(); 118 } 119 return false; 120 } 25 public boolean create(Tag tag) throws ProxyAuthenticationException; 121 26 122 27 /** … … 127 32 * @throws ProxyAuthenticationException 128 33 */ 129 public List<Tag> tagsForImage(Image image) throws IOException, ProxyAuthenticationException { 130 Document doc = sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id", String 131 .valueOf(image.getIdentifier())); 132 Element elementImage = (Element) doc.getDocumentElement().getElementsByTagName("image").item(0); 133 Element elementTag = (Element) elementImage.getElementsByTagName("tags").item(0); 134 return getTagsFromDocument(elementTag); 135 } 136 34 public List<Tag> tagsForImage(Image image) throws IOException, ProxyAuthenticationException; 137 35 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/CategoryDaoImpl.java
r9902 r9919 1 package fr.mael.jiwigo.dao ;1 package fr.mael.jiwigo.dao.impl; 2 2 3 3 import java.io.IOException; … … 5 5 import java.util.List; 6 6 7 import org.slf4j.Logger; 8 import org.slf4j.LoggerFactory; 7 9 import org.w3c.dom.Document; 8 10 import org.w3c.dom.Element; … … 10 12 import org.w3c.dom.NodeList; 11 13 14 import fr.mael.jiwigo.dao.CategoryDao; 12 15 import fr.mael.jiwigo.om.Category; 13 16 import fr.mael.jiwigo.transverse.enumeration.CategoryEnum; … … 34 37 * 35 38 */ 36 public class CategoryDao {39 public class CategoryDaoImpl implements CategoryDao { 37 40 /** 38 41 * Logger 39 42 */ 40 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 41 .getLog(CategoryDao.class); 43 private final Logger LOG = LoggerFactory.getLogger(CategoryDaoImpl.class); 42 44 /** 43 45 * Instance to use a singleton 44 46 */ 45 private static CategoryDao instance;47 private static CategoryDaoImpl instance; 46 48 47 49 private SessionManager sessionManager; 48 49 /**50 * Private constructor to use a singleton51 */52 private CategoryDao(SessionManager sessionManager) {53 this.sessionManager = sessionManager;54 }55 56 /**57 * @return the instance58 */59 public static CategoryDao getInstance(SessionManager sessionManager) {60 if (instance == null) {61 instance = new CategoryDao(sessionManager);62 }63 return instance;64 }65 50 66 51 /** … … 125 110 } 126 111 112 public SessionManager getSessionManager() { 113 return sessionManager; 114 } 115 116 public void setSessionManager(SessionManager sessionManager) { 117 this.sessionManager = sessionManager; 118 } 119 127 120 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/CommentDaoImpl.java
r9902 r9919 1 package fr.mael.jiwigo.dao ;1 package fr.mael.jiwigo.dao.impl; 2 2 3 3 import java.io.IOException; … … 5 5 import java.util.List; 6 6 7 import org.slf4j.Logger; 8 import org.slf4j.LoggerFactory; 7 9 import org.w3c.dom.Document; 8 10 import org.w3c.dom.Element; … … 10 12 import org.w3c.dom.NodeList; 11 13 14 import fr.mael.jiwigo.dao.CommentDao; 12 15 import fr.mael.jiwigo.om.Comment; 13 16 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; … … 34 37 * 35 38 */ 36 public class CommentDao {39 public class CommentDaoImpl implements CommentDao { 37 40 /** 38 41 * Logger 39 42 */ 40 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 41 .getLog(CommentDao.class); 42 /** 43 * Instance that allows to use a singleton 44 */ 45 private static CommentDao instance; 43 private final Logger LOG = LoggerFactory.getLogger(CommentDaoImpl.class); 46 44 47 45 private SessionManager sessionManager; 48 49 /**50 * private constructor, to use a singleton51 */52 private CommentDao(SessionManager sessionManager) {53 this.sessionManager = sessionManager;54 }55 56 /**57 * @return the singleton58 */59 public static CommentDao getInstance(SessionManager sessionManager) {60 if (instance == null) {61 instance = new CommentDao(sessionManager);62 }63 return instance;64 }65 46 66 47 /** … … 133 114 134 115 } 116 117 public SessionManager getSessionManager() { 118 return sessionManager; 119 } 120 121 public void setSessionManager(SessionManager sessionManager) { 122 this.sessionManager = sessionManager; 123 } 124 135 125 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/ImageDaoImpl.java
r9902 r9919 1 package fr.mael.jiwigo.dao ;1 package fr.mael.jiwigo.dao.impl; 2 2 3 3 import java.io.File; … … 8 8 import java.util.List; 9 9 10 import org.slf4j.Logger; 11 import org.slf4j.LoggerFactory; 10 12 import org.w3c.dom.Document; 11 13 import org.w3c.dom.Element; … … 14 16 15 17 import sun.misc.BASE64Encoder; 18 import fr.mael.jiwigo.dao.ImageDao; 16 19 import fr.mael.jiwigo.om.Image; 17 20 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; … … 39 42 * 40 43 */ 41 public class ImageDao {44 public class ImageDaoImpl implements ImageDao { 42 45 43 46 /** 44 47 * Logger 45 48 */ 46 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 47 .getLog(ImageDao.class); 48 49 /** 50 * Singleton 51 */ 52 private static ImageDao instance; 49 private final Logger LOG = LoggerFactory.getLogger(ImageDaoImpl.class); 53 50 54 51 /** … … 66 63 private ArrayList<File> filesToSend; 67 64 68 /** 69 * Private singleton, to use a singleton 70 */ 71 private ImageDao(SessionManager sessionManager) { 65 public ImageDaoImpl() { 72 66 cache = new HashMap<Integer, List<Image>>(); 73 this.sessionManager = sessionManager;74 }75 76 /**77 * @return le singleton78 */79 public static ImageDao getInstance(SessionManager sessionManager) {80 if (instance == null) {81 instance = new ImageDao(sessionManager);82 }83 return instance;84 67 } 85 68 … … 260 243 public List<Image> search(String searchString) throws IOException, ProxyAuthenticationException { 261 244 Document doc = sessionManager.executeReturnDocument(MethodsEnum.SEARCH.getLabel(), "query", searchString); 262 LOG.debug( doc);245 LOG.debug(Tools.documentToString(doc)); 263 246 Element element = (Element) doc.getDocumentElement().getElementsByTagName("images").item(0); 264 247 return getImagesFromElement(element); … … 277 260 } 278 261 262 public SessionManager getSessionManager() { 263 return sessionManager; 264 } 265 266 public void setSessionManager(SessionManager sessionManager) { 267 this.sessionManager = sessionManager; 268 } 269 279 270 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/TagDaoImpl.java
r9902 r9919 1 package fr.mael.jiwigo.dao ;1 package fr.mael.jiwigo.dao.impl; 2 2 3 3 import java.io.IOException; … … 5 5 import java.util.List; 6 6 7 import org.slf4j.Logger; 8 import org.slf4j.LoggerFactory; 7 9 import org.w3c.dom.Document; 8 10 import org.w3c.dom.Element; … … 10 12 import org.w3c.dom.NodeList; 11 13 14 import fr.mael.jiwigo.dao.TagDao; 12 15 import fr.mael.jiwigo.om.Image; 13 16 import fr.mael.jiwigo.om.Tag; … … 35 38 * 36 39 */ 37 public class TagDao {40 public class TagDaoImpl implements TagDao { 38 41 /** 39 42 * Logger 40 43 */ 41 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(TagDao.class); 42 /** 43 * Instance, to use a singleton 44 */ 45 private static TagDao instance; 44 private final Logger LOG = LoggerFactory.getLogger(ImageDaoImpl.class); 46 45 47 46 private SessionManager sessionManager; 48 49 /**50 * private constructor to use a singleton51 */52 private TagDao(SessionManager sessionManager) {53 this.sessionManager = sessionManager;54 }55 56 /**57 * @return the instance58 */59 public static TagDao getInstance(SessionManager sessionManager) {60 if (instance == null) {61 instance = new TagDao(sessionManager);62 }63 return instance;64 }65 47 66 48 /** … … 135 117 } 136 118 119 public SessionManager getSessionManager() { 120 return sessionManager; 121 } 122 123 public void setSessionManager(SessionManager sessionManager) { 124 this.sessionManager = sessionManager; 125 } 126 137 127 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/CategoryService.java
r9879 r9919 4 4 import java.util.List; 5 5 6 import fr.mael.jiwigo.dao.CategoryDao;7 6 import fr.mael.jiwigo.om.Category; 8 7 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 9 import fr.mael.jiwigo.transverse.session.SessionManager;10 8 11 /* 12 * jiwigo-ws-api Piwigo webservice access Api 13 * Copyright (c) 2010-2011 Mael mael@le-guevel.com 14 * All Rights Reserved 15 * 16 * This library is free software. It comes without any warranty, to 17 * the extent permitted by applicable law. You can redistribute it 18 * and/or modify it under the terms of the Do What The Fuck You Want 19 * To Public License, Version 2, as published by Sam Hocevar. See 20 * http://sam.zoy.org/wtfpl/COPYING for more details. 21 */ 22 /** 23 24 * @author mael 25 * 26 */ 27 public class CategoryService { 28 /** 29 * Logger 30 */ 31 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 32 .getLog(CategoryService.class); 33 34 /** 35 * singleton 36 */ 37 private static CategoryService instance; 38 39 private SessionManager sessionManager; 40 41 /** 42 * @return the singleton 43 */ 44 public static CategoryService getInstance(SessionManager sessionManager) { 45 if (instance == null) { 46 instance = new CategoryService(sessionManager); 47 } 48 return instance; 49 } 50 51 /** 52 * private constructor to use a singleton 53 */ 54 private CategoryService(SessionManager sessionManager) { 55 this.sessionManager = sessionManager; 56 57 } 58 9 public interface CategoryService { 59 10 /** 60 11 * Lists all categories … … 64 15 * @throws ProxyAuthenticationException 65 16 */ 66 public List<Category> list(boolean recursive) throws IOException, ProxyAuthenticationException { 67 return CategoryDao.getInstance(sessionManager).list(recursive); 68 } 17 public List<Category> list(boolean recursive) throws IOException, ProxyAuthenticationException; 69 18 70 19 /** … … 74 23 * @throws ProxyAuthenticationException 75 24 */ 76 public List<Category> makeTree() throws IOException, ProxyAuthenticationException { 77 List<Category> list = CategoryDao.getInstance(sessionManager).list(true); 78 for (Category category : list) { 79 for (Category category2 : list) { 80 if (category2.getIdParentCategories().size() != 1 81 && category.getIdentifier().equals( 82 category2.getIdParentCategories().get(category2.getIdParentCategories().size() - 2))) { 83 category.getChildCategories().add(category2); 84 category2.getParentCategories().add(category); 85 } 86 87 } 88 } 89 90 return list; 91 92 } 25 public List<Category> makeTree() throws IOException, ProxyAuthenticationException; 93 26 94 27 /** … … 99 32 * @throws ProxyAuthenticationException 100 33 */ 101 public boolean create(String nom, Integer parent) throws ProxyAuthenticationException { 102 Category category = new Category(); 103 category.setDirectParent(parent); 104 category.setName(nom); 105 return CategoryDao.getInstance(sessionManager).create(category); 106 } 34 public boolean create(String nom, Integer parent) throws ProxyAuthenticationException; 107 35 108 36 /** … … 112 40 * @throws ProxyAuthenticationException 113 41 */ 114 public boolean create(String nom) throws ProxyAuthenticationException { 115 Category category = new Category(); 116 category.setName(nom); 117 return CategoryDao.getInstance(sessionManager).create(category); 118 } 42 public boolean create(String nom) throws ProxyAuthenticationException; 119 43 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/CommentService.java
r9879 r9919 4 4 import java.util.List; 5 5 6 import fr.mael.jiwigo.dao.CommentDao;7 6 import fr.mael.jiwigo.om.Comment; 8 7 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 9 import fr.mael.jiwigo.transverse.session.SessionManager;10 8 11 /* 12 * jiwigo-ws-api Piwigo webservice access Api 13 * Copyright (c) 2010-2011 Mael mael@le-guevel.com 14 * All Rights Reserved 15 * 16 * This library is free software. It comes without any warranty, to 17 * the extent permitted by applicable law. You can redistribute it 18 * and/or modify it under the terms of the Do What The Fuck You Want 19 * To Public License, Version 2, as published by Sam Hocevar. See 20 * http://sam.zoy.org/wtfpl/COPYING for more details. 21 */ 22 /** 23 24 * @author mael 25 * 26 */ 27 public class CommentService { 28 /** 29 * Logger 30 */ 31 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 32 .getLog(CommentService.class); 33 34 /** 35 * singleton 36 */ 37 private static CommentService instance; 38 39 private SessionManager sessionManager; 40 41 /** 42 * @return the singleton 43 */ 44 public static CommentService getInstance(SessionManager sessionManager) { 45 if (instance == null) { 46 instance = new CommentService(sessionManager); 47 } 48 return instance; 49 } 50 51 /** 52 * private constructor, to use a singleton 53 */ 54 private CommentService(SessionManager sessionManager) { 55 this.sessionManager = sessionManager; 56 } 57 9 public interface CommentService { 58 10 /** 59 11 * Lists all comments for an image … … 63 15 * @throws ProxyAuthenticationException 64 16 */ 65 public List<Comment> list(Integer imageId) throws IOException, ProxyAuthenticationException { 66 return CommentDao.getInstance(sessionManager).list(imageId); 67 } 17 public List<Comment> list(Integer imageId) throws IOException, ProxyAuthenticationException; 68 18 69 19 /** … … 77 27 */ 78 28 public boolean create(String content, Integer imageId, String auteur) throws IOException, 79 ProxyAuthenticationException { 80 Comment comment = new Comment(); 81 comment.setContent(content); 82 comment.setImageId(imageId); 83 comment.setAuthor(auteur); 84 return CommentDao.getInstance(sessionManager).create(comment); 85 } 29 ProxyAuthenticationException; 86 30 87 31 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/ImageService.java
r9893 r9919 1 1 package fr.mael.jiwigo.service; 2 2 3 import java.io.File;4 3 import java.io.IOException; 5 4 import java.security.NoSuchAlgorithmException; 6 5 import java.util.List; 7 6 8 import fr.mael.jiwigo.dao.ImageDao;9 7 import fr.mael.jiwigo.om.Image; 10 8 import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; 11 9 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 12 10 import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException; 13 import fr.mael.jiwigo.transverse.session.SessionManager;14 import fr.mael.jiwigo.transverse.util.ImagesUtil;15 import fr.mael.jiwigo.transverse.util.Tools;16 11 17 /* 18 * jiwigo-ws-api Piwigo webservice access Api 19 * Copyright (c) 2010-2011 Mael mael@le-guevel.com 20 * All Rights Reserved 21 * 22 * This library is free software. It comes without any warranty, to 23 * the extent permitted by applicable law. You can redistribute it 24 * and/or modify it under the terms of the Do What The Fuck You Want 25 * To Public License, Version 2, as published by Sam Hocevar. See 26 * http://sam.zoy.org/wtfpl/COPYING for more details. 27 */ 28 /** 29 * 30 31 * @author mael 32 * 33 */ 34 public class ImageService { 35 36 /** 37 * Logger 38 */ 39 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 40 .getLog(ImageService.class); 41 42 /** 43 * Singleton 44 */ 45 private static ImageService instance; 46 47 private SessionManager sessionManager; 48 49 /** 50 * @return the singleton 51 */ 52 public static ImageService getInstance(SessionManager sessionManager) { 53 if (instance == null) { 54 instance = new ImageService(sessionManager); 55 } 56 return instance; 57 } 58 59 /** 60 * private constructor to use a singleton 61 */ 62 private ImageService(SessionManager sessionManager) { 63 this.sessionManager = sessionManager; 64 } 12 public interface ImageService { 65 13 66 14 /** … … 72 20 */ 73 21 public List<Image> listByCategory(Integer categoryId, boolean rafraichir) throws IOException, 74 ProxyAuthenticationException { 75 return ImageDao.getInstance(sessionManager).listByCategory(categoryId, rafraichir); 76 } 22 ProxyAuthenticationException; 77 23 78 24 /** … … 92 38 public boolean create(String filePath, Integer idCategory, Integer originalWidth, Integer originalHeight, 93 39 Double chunckSize, Integer privacyLevel) throws IOException, NoSuchAlgorithmException, 94 FileAlreadyExistsException, ProxyAuthenticationException, WrongChunkSizeException { 95 File originalFile = new File(filePath); 96 //get the byte array of the original file, to keep metadata 97 byte[] bytesFichierOriginal = Tools.getBytesFromFile(originalFile); 98 99 //resize the picture (or not) 100 boolean originaleRedimensionnee = ImagesUtil.scale(filePath, "originale.jpg", originalWidth, originalHeight); 101 //create the thumbnail 102 ImagesUtil.scale(filePath, "thumb.jpg", 120, 90); 103 //get the thumbnail 104 File thumbnail = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg"); 105 File originale = null; 106 if (originaleRedimensionnee) { 107 originale = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg"); 108 //if the original file has been resized, we put the metadata in the resized file 109 //I use here a try catch because if the original file isn't a jpeg 110 //the methode Outil.enrich will fail, but the procedure has to continue 111 try { 112 byte[] fichierEnrichi = Tools.enrich(bytesFichierOriginal, Tools.getBytesFromFile(new File(System 113 .getProperty("java.io.tmpdir") 114 + "/originale.jpg"))); 115 Tools.byteToFile(System.getProperty("java.io.tmpdir") + "/originale.jpg", fichierEnrichi); 116 } catch (Exception e) { 117 } 118 } else { 119 originale = originalFile; 120 121 } 122 Image image = new Image(); 123 image.setName(getImageName(filePath)); 124 image.setThumbnail(thumbnail); 125 image.setOriginale(originale); 126 image.setIdCategory(idCategory); 127 image.setPrivacyLevel(String.valueOf(privacyLevel)); 128 //now we call the dao to send the image to the webservice 129 return ImageDao.getInstance(sessionManager).create(image, chunckSize * 1000000); 130 } 40 FileAlreadyExistsException, ProxyAuthenticationException, WrongChunkSizeException; 131 41 132 42 /** … … 138 48 * @throws ProxyAuthenticationException 139 49 */ 140 public boolean addTags(Image image, String tagId) throws IOException, ProxyAuthenticationException { 141 return ImageDao.getInstance(sessionManager).addTags(image.getIdentifier(), tagId); 142 } 50 public boolean addTags(Image image, String tagId) throws IOException, ProxyAuthenticationException; 143 51 144 52 /** … … 149 57 * @throws ProxyAuthenticationException 150 58 */ 151 public List<Image> search(String queryString) throws IOException, ProxyAuthenticationException { 152 return ImageDao.getInstance(sessionManager).search(queryString); 153 } 154 155 /** 156 * Deletes the file extension 157 * @param path 158 * @return 159 */ 160 private String getImageName(String path) { 161 File fichier = new File(path); 162 StringBuffer name = new StringBuffer(fichier.getName()); 163 return (name.delete(name.lastIndexOf("."), name.length())).toString(); 164 165 } 59 public List<Image> search(String queryString) throws IOException, ProxyAuthenticationException; 166 60 167 61 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/TagService.java
r9879 r9919 4 4 import java.util.List; 5 5 6 import fr.mael.jiwigo.dao.TagDao;7 6 import fr.mael.jiwigo.om.Image; 8 7 import fr.mael.jiwigo.om.Tag; 9 8 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 10 import fr.mael.jiwigo.transverse.session.SessionManager;11 9 12 /* 13 * jiwigo-ws-api Piwigo webservice access Api 14 * Copyright (c) 2010-2011 Mael mael@le-guevel.com 15 * All Rights Reserved 16 * 17 * This library is free software. It comes without any warranty, to 18 * the extent permitted by applicable law. You can redistribute it 19 * and/or modify it under the terms of the Do What The Fuck You Want 20 * To Public License, Version 2, as published by Sam Hocevar. See 21 * http://sam.zoy.org/wtfpl/COPYING for more details. 22 */ 23 /** 24 * 25 26 * @author mael 27 * 28 */ 29 public class TagService { 30 /** 31 * Logger 32 */ 33 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 34 .getLog(TagService.class); 35 36 /** 37 * The instance, to use a singleton 38 */ 39 private static TagService instance; 40 41 private SessionManager sessionManager; 42 43 /** 44 * @return the singleton 45 */ 46 public static TagService getInstance(SessionManager sessionManager) { 47 if (instance == null) { 48 instance = new TagService(sessionManager); 49 } 50 return instance; 51 } 52 53 /** 54 * private constructor 55 */ 56 private TagService(SessionManager sessionManager) { 57 this.sessionManager = sessionManager; 58 } 10 public interface TagService { 59 11 60 12 /** … … 64 16 * @throws ProxyAuthenticationException 65 17 */ 66 public List<Tag> list() throws IOException, ProxyAuthenticationException { 67 return TagDao.getInstance(sessionManager).list(); 68 } 18 public List<Tag> list() throws IOException, ProxyAuthenticationException; 69 19 70 20 /** … … 75 25 * @throws ProxyAuthenticationException 76 26 */ 77 public boolean create(String nom) throws IOException, ProxyAuthenticationException { 78 Tag tag = new Tag(); 79 tag.setName(nom); 80 return TagDao.getInstance(sessionManager).create(tag); 81 } 27 public boolean create(String nom) throws IOException, ProxyAuthenticationException; 82 28 83 29 /** … … 88 34 * @throws ProxyAuthenticationException 89 35 */ 90 public List<Tag> tagsForImage(Image image) throws IOException, ProxyAuthenticationException { 91 return TagDao.getInstance(sessionManager).tagsForImage(image); 92 } 93 36 public List<Tag> tagsForImage(Image image) throws IOException, ProxyAuthenticationException; 94 37 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/impl/CategoryServiceImpl.java
r9879 r9919 1 package fr.mael.jiwigo.service ;1 package fr.mael.jiwigo.service.impl; 2 2 3 3 import java.io.IOException; 4 4 import java.util.List; 5 5 6 import org.slf4j.Logger; 7 import org.slf4j.LoggerFactory; 8 6 9 import fr.mael.jiwigo.dao.CategoryDao; 7 10 import fr.mael.jiwigo.om.Category; 11 import fr.mael.jiwigo.service.CategoryService; 8 12 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 9 import fr.mael.jiwigo.transverse.session.SessionManager;10 13 11 14 /* … … 25 28 * 26 29 */ 27 public class CategoryService {30 public class CategoryServiceImpl implements CategoryService { 28 31 /** 29 32 * Logger 30 33 */ 31 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 32 .getLog(CategoryService.class); 34 private final Logger LOG = LoggerFactory.getLogger(CategoryServiceImpl.class); 33 35 34 /** 35 * singleton 36 */ 37 private static CategoryService instance; 38 39 private SessionManager sessionManager; 40 41 /** 42 * @return the singleton 43 */ 44 public static CategoryService getInstance(SessionManager sessionManager) { 45 if (instance == null) { 46 instance = new CategoryService(sessionManager); 47 } 48 return instance; 49 } 50 51 /** 52 * private constructor to use a singleton 53 */ 54 private CategoryService(SessionManager sessionManager) { 55 this.sessionManager = sessionManager; 56 57 } 36 private CategoryDao dao; 58 37 59 38 /** … … 65 44 */ 66 45 public List<Category> list(boolean recursive) throws IOException, ProxyAuthenticationException { 67 return CategoryDao.getInstance(sessionManager).list(recursive);46 return dao.list(recursive); 68 47 } 69 48 … … 75 54 */ 76 55 public List<Category> makeTree() throws IOException, ProxyAuthenticationException { 77 List<Category> list = CategoryDao.getInstance(sessionManager).list(true);56 List<Category> list = dao.list(true); 78 57 for (Category category : list) { 79 58 for (Category category2 : list) { … … 103 82 category.setDirectParent(parent); 104 83 category.setName(nom); 105 return CategoryDao.getInstance(sessionManager).create(category);84 return dao.create(category); 106 85 } 107 86 … … 115 94 Category category = new Category(); 116 95 category.setName(nom); 117 return CategoryDao.getInstance(sessionManager).create(category);96 return dao.create(category); 118 97 } 98 99 public CategoryDao getDao() { 100 return dao; 101 } 102 103 public void setDao(CategoryDao dao) { 104 this.dao = dao; 105 } 106 119 107 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/impl/CommentServiceImpl.java
r9879 r9919 1 package fr.mael.jiwigo.service ;1 package fr.mael.jiwigo.service.impl; 2 2 3 3 import java.io.IOException; 4 4 import java.util.List; 5 5 6 import org.slf4j.Logger; 7 import org.slf4j.LoggerFactory; 8 6 9 import fr.mael.jiwigo.dao.CommentDao; 7 10 import fr.mael.jiwigo.om.Comment; 11 import fr.mael.jiwigo.service.CommentService; 8 12 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 9 import fr.mael.jiwigo.transverse.session.SessionManager;10 13 11 14 /* … … 25 28 * 26 29 */ 27 public class CommentService {30 public class CommentServiceImpl implements CommentService { 28 31 /** 29 32 * Logger 30 33 */ 31 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 32 .getLog(CommentService.class); 34 private final Logger LOG = LoggerFactory.getLogger(CommentServiceImpl.class); 33 35 34 /** 35 * singleton 36 */ 37 private static CommentService instance; 38 39 private SessionManager sessionManager; 40 41 /** 42 * @return the singleton 43 */ 44 public static CommentService getInstance(SessionManager sessionManager) { 45 if (instance == null) { 46 instance = new CommentService(sessionManager); 47 } 48 return instance; 49 } 50 51 /** 52 * private constructor, to use a singleton 53 */ 54 private CommentService(SessionManager sessionManager) { 55 this.sessionManager = sessionManager; 56 } 36 private CommentDao dao; 57 37 58 38 /** … … 64 44 */ 65 45 public List<Comment> list(Integer imageId) throws IOException, ProxyAuthenticationException { 66 return CommentDao.getInstance(sessionManager).list(imageId);46 return dao.list(imageId); 67 47 } 68 48 … … 82 62 comment.setImageId(imageId); 83 63 comment.setAuthor(auteur); 84 return CommentDao.getInstance(sessionManager).create(comment); 64 return dao.create(comment); 65 } 66 67 public CommentDao getDao() { 68 return dao; 69 } 70 71 public void setDao(CommentDao dao) { 72 this.dao = dao; 85 73 } 86 74 -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/impl/ImageServiceImpl.java
r9893 r9919 1 package fr.mael.jiwigo.service ;1 package fr.mael.jiwigo.service.impl; 2 2 3 3 import java.io.File; … … 6 6 import java.util.List; 7 7 8 import org.slf4j.Logger; 9 import org.slf4j.LoggerFactory; 10 8 11 import fr.mael.jiwigo.dao.ImageDao; 9 12 import fr.mael.jiwigo.om.Image; 13 import fr.mael.jiwigo.service.ImageService; 10 14 import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; 11 15 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 12 16 import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException; 13 import fr.mael.jiwigo.transverse.session.SessionManager;14 17 import fr.mael.jiwigo.transverse.util.ImagesUtil; 15 18 import fr.mael.jiwigo.transverse.util.Tools; … … 32 35 * 33 36 */ 34 public class ImageService {37 public class ImageServiceImpl implements ImageService { 35 38 36 39 /** 37 40 * Logger 38 41 */ 39 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 40 .getLog(ImageService.class); 42 private final Logger LOG = LoggerFactory.getLogger(ImageServiceImpl.class); 41 43 42 /** 43 * Singleton 44 */ 45 private static ImageService instance; 46 47 private SessionManager sessionManager; 48 49 /** 50 * @return the singleton 51 */ 52 public static ImageService getInstance(SessionManager sessionManager) { 53 if (instance == null) { 54 instance = new ImageService(sessionManager); 55 } 56 return instance; 57 } 58 59 /** 60 * private constructor to use a singleton 61 */ 62 private ImageService(SessionManager sessionManager) { 63 this.sessionManager = sessionManager; 64 } 44 private ImageDao dao; 65 45 66 46 /** … … 73 53 public List<Image> listByCategory(Integer categoryId, boolean rafraichir) throws IOException, 74 54 ProxyAuthenticationException { 75 return ImageDao.getInstance(sessionManager).listByCategory(categoryId, rafraichir);55 return dao.listByCategory(categoryId, rafraichir); 76 56 } 77 57 … … 127 107 image.setPrivacyLevel(String.valueOf(privacyLevel)); 128 108 //now we call the dao to send the image to the webservice 129 return ImageDao.getInstance(sessionManager).create(image, chunckSize * 1000000);109 return dao.create(image, chunckSize * 1000000); 130 110 } 131 111 … … 139 119 */ 140 120 public boolean addTags(Image image, String tagId) throws IOException, ProxyAuthenticationException { 141 return ImageDao.getInstance(sessionManager).addTags(image.getIdentifier(), tagId);121 return dao.addTags(image.getIdentifier(), tagId); 142 122 } 143 123 … … 150 130 */ 151 131 public List<Image> search(String queryString) throws IOException, ProxyAuthenticationException { 152 return ImageDao.getInstance(sessionManager).search(queryString);132 return dao.search(queryString); 153 133 } 154 134 … … 165 145 } 166 146 147 public ImageDao getDao() { 148 return dao; 149 } 150 151 public void setDao(ImageDao dao) { 152 this.dao = dao; 153 } 154 167 155 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/impl/TagServiceImpl.java
r9879 r9919 1 package fr.mael.jiwigo.service ;1 package fr.mael.jiwigo.service.impl; 2 2 3 3 import java.io.IOException; 4 4 import java.util.List; 5 5 6 import org.slf4j.Logger; 7 import org.slf4j.LoggerFactory; 8 6 9 import fr.mael.jiwigo.dao.TagDao; 7 10 import fr.mael.jiwigo.om.Image; 8 11 import fr.mael.jiwigo.om.Tag; 12 import fr.mael.jiwigo.service.TagService; 9 13 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 10 import fr.mael.jiwigo.transverse.session.SessionManager;11 14 12 15 /* … … 27 30 * 28 31 */ 29 public class TagService {32 public class TagServiceImpl implements TagService { 30 33 /** 31 34 * Logger 32 35 */ 33 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 34 .getLog(TagService.class); 36 private final Logger LOG = LoggerFactory.getLogger(TagServiceImpl.class); 35 37 36 /** 37 * The instance, to use a singleton 38 */ 39 private static TagService instance; 40 41 private SessionManager sessionManager; 42 43 /** 44 * @return the singleton 45 */ 46 public static TagService getInstance(SessionManager sessionManager) { 47 if (instance == null) { 48 instance = new TagService(sessionManager); 49 } 50 return instance; 51 } 52 53 /** 54 * private constructor 55 */ 56 private TagService(SessionManager sessionManager) { 57 this.sessionManager = sessionManager; 58 } 38 private TagDao dao; 59 39 60 40 /** … … 65 45 */ 66 46 public List<Tag> list() throws IOException, ProxyAuthenticationException { 67 return TagDao.getInstance(sessionManager).list();47 return dao.list(); 68 48 } 69 49 … … 78 58 Tag tag = new Tag(); 79 59 tag.setName(nom); 80 return TagDao.getInstance(sessionManager).create(tag);60 return dao.create(tag); 81 61 } 82 62 … … 89 69 */ 90 70 public List<Tag> tagsForImage(Image image) throws IOException, ProxyAuthenticationException { 91 return TagDao.getInstance(sessionManager).tagsForImage(image); 71 return dao.tagsForImage(image); 72 } 73 74 public TagDao getDao() { 75 return dao; 76 } 77 78 public void setDao(TagDao dao) { 79 this.dao = dao; 92 80 } 93 81 -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/transverse/session/SessionManager.java
r9902 r9919 2 2 3 3 import java.io.IOException; 4 import java.io.InputStream;5 4 import java.io.UnsupportedEncodingException; 6 import java.util.ArrayList;7 import java.util.List;8 5 9 import org.apache.commons.lang.StringUtils;10 import org.apache.http.HttpHost;11 import org.apache.http.HttpResponse;12 import org.apache.http.NameValuePair;13 import org.apache.http.auth.AuthScope;14 import org.apache.http.auth.UsernamePasswordCredentials;15 import org.apache.http.client.entity.UrlEncodedFormEntity;16 import org.apache.http.client.methods.HttpPost;17 import org.apache.http.conn.params.ConnRoutePNames;18 import org.apache.http.impl.client.DefaultHttpClient;19 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;20 import org.apache.http.message.BasicNameValuePair;21 6 import org.w3c.dom.Document; 22 7 23 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;24 8 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 25 import fr.mael.jiwigo.transverse.util.Tools;26 9 27 /* 28 * jiwigo-ws-api Piwigo webservice access Api 29 * Copyright (c) 2010-2011 Mael mael@le-guevel.com 30 * All Rights Reserved 31 * 32 * This library is free software. It comes without any warranty, to 33 * the extent permitted by applicable law. You can redistribute it 34 * and/or modify it under the terms of the Do What The Fuck You Want 35 * To Public License, Version 2, as published by Sam Hocevar. See 36 * http://sam.zoy.org/wtfpl/COPYING for more details. 37 */ 38 /** 39 40 * @author mael 41 * Gestionnaire de connexion 42 */ 43 public class SessionManager { 44 45 /** 46 * Logger 47 */ 48 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 49 .getLog(SessionManager.class); 50 /** 51 * the entered login 52 */ 53 private String login; 54 /** 55 * the entered password 56 */ 57 private String password; 58 /** 59 * the url of the site 60 */ 61 private String url; 62 /** 63 * the http client 64 */ 65 private DefaultHttpClient client; 66 67 /** 68 * defines if the user uses a proxy 69 */ 70 private boolean usesProxy; 71 72 /** 73 * url of the proxy 74 */ 75 private String urlProxy; 76 77 /** 78 * port of the proxy 79 */ 80 private int portProxy; 81 82 /** 83 * login for the proxy 84 */ 85 private String loginProxy; 86 87 /** 88 * pass for the proxy 89 */ 90 private String passProxy; 91 92 /** 93 * true : an error was found for the proxy 94 */ 95 private boolean proxyError; 96 97 /** 98 * Constructor 99 * @param login the login 100 * @param password the password 101 * @param url the url of the site 102 */ 103 public SessionManager(String login, String password, String url) { 104 this.login = login; 105 this.password = password; 106 this.url = url + "/ws.php"; 107 // MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); 108 // client = new HttpClient(connectionManager); 109 ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(); 110 client = new DefaultHttpClient(connectionManager); 111 //Using of a Linux user agent. cause... it's better 8) 112 client.getParams().setParameter("http.useragent", 113 "Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1"); 114 115 } 116 10 public interface SessionManager { 117 11 /** 118 12 * Connection method … … 122 16 * 123 17 */ 124 public int processLogin() { 125 Document doc; 126 //configures the proxy 127 if (usesProxy) { 128 // HostConfiguration config = client.getHostConfiguration(); 129 // config.setProxy(urlProxy, portProxy); 130 if (!StringUtils.isEmpty(loginProxy) && !StringUtils.isEmpty(passProxy)) { 131 // Credentials credentials = new UsernamePasswordCredentials(loginProxy, passProxy); 132 // AuthScope authScope = new AuthScope(urlProxy, portProxy); 133 // client.getState().setProxyCredentials(authScope, credentials); 134 HttpHost proxy = new HttpHost(urlProxy, portProxy); 135 client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); 136 client.getCredentialsProvider().setCredentials(new AuthScope(urlProxy, portProxy), 137 new UsernamePasswordCredentials(loginProxy, passProxy)); 138 139 } 140 } 141 // try { 142 try { 143 doc = executeReturnDocument(MethodsEnum.LOGIN.getLabel(), "username", login, "password", password); 144 return (Tools.checkOk(doc) ? 0 : 1); 145 } catch (IOException e) { 146 LOG.debug(Tools.getStackTrace(e)); 147 return 1; 148 } catch (ProxyAuthenticationException e) { 149 LOG.debug(Tools.getStackTrace(e)); 150 return 2; 151 } catch (Exception e) { 152 LOG.debug(Tools.getStackTrace(e)); 153 return 1; 154 } 155 156 } 18 public int processLogin(); 157 19 158 20 /** … … 165 27 */ 166 28 public String executeReturnString(String methode, String... parametres) throws UnsupportedEncodingException, 167 ProxyAuthenticationException { 168 if (parametres.length % 2 != 0 && !(parametres == null)) { 169 try { 170 throw new Exception("Le nombre de parametres doit etre pair"); 171 } catch (Exception e) { 172 LOG.error(Tools.getStackTrace(e)); 173 return null; 174 } 175 } 176 HttpPost method = new HttpPost(url); 177 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 178 nameValuePairs.add(new BasicNameValuePair("method", methode)); 179 for (int i = 0; i < parametres.length; i += 2) { 180 nameValuePairs.add(new BasicNameValuePair(parametres[i], parametres[i + 1])); 181 } 182 method.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 183 //begin bug:0001833 184 method.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); 185 //end 186 try { 187 HttpResponse response = client.execute(method); 188 if (response.getStatusLine().getStatusCode() == 407) { 189 throw new ProxyAuthenticationException("Error while trying to auth on the proxy"); 190 } 191 InputStream inputStream = response.getEntity().getContent(); 192 StringBuffer buffer = new StringBuffer(); 193 byte[] data = new byte[512]; 194 int len = 0; 195 try { 196 while (-1 != (len = inputStream.read(data))) { 197 buffer.append(new String(data, 0, len)); 198 } 199 } catch (IOException e) { 200 e.printStackTrace(); 201 } 202 try { 203 inputStream.close(); 204 } catch (IOException e) { 205 e.printStackTrace(); 206 } 207 return buffer.toString(); 208 } catch (IOException e) { 209 210 } 211 return null; 212 213 } 29 ProxyAuthenticationException; 214 30 215 31 /** … … 222 38 */ 223 39 public Document executeReturnDocument(String methode, String... parametres) throws IOException, 224 ProxyAuthenticationException { 225 try { 226 String returnedString = executeReturnString(methode, parametres); 227 return Tools.stringToDocument(returnedString); 228 } catch (Exception e) { 229 // TODO Auto-generated catch block 230 LOG.error(Tools.getStackTrace(e)); 231 } 232 return null; 233 234 } 40 ProxyAuthenticationException; 235 41 236 42 /** … … 240 46 * @throws ProxyAuthenticationException 241 47 */ 242 public Document executeReturnDocument(String methode) throws ProxyAuthenticationException { 243 try { 244 return Tools.stringToDocument(executeReturnString(methode)); 245 } catch (Exception e) { 246 // TODO Auto-generated catch block 247 LOG.error(Tools.getStackTrace(e)); 248 } 249 return null; 250 251 } 48 public Document executeReturnDocument(String methode) throws ProxyAuthenticationException; 252 49 253 50 /** 51 * Getter of the login 254 52 * @return the login 255 53 */ 256 public String getLogin() { 257 return login; 258 } 54 public String getLogin(); 259 55 260 56 /** 261 * @param login the login to set 57 * Setter of the login 58 * @param login 262 59 */ 263 public void setLogin(String login) { 264 this.login = login; 265 } 60 public void setLogin(String login); 266 61 267 62 /** 268 * @return the url 63 * Setter of the user agent 64 * @param userAgent 269 65 */ 270 public String getUrl() { 271 return url; 272 } 66 public void setUserAgent(String userAgent); 273 67 274 68 /** 275 * @param url the url to set 69 * Setter of the password to access piwigo 70 * @param password 276 71 */ 277 public void setUrl(String url) { 278 this.url = url; 279 } 72 public void setPassword(String password); 280 73 281 74 /** 282 * @param usesProxy the usesProxy to set 75 * Setter of the proxy login 76 * @param loginProxy 283 77 */ 284 public void setUsesProxy(boolean usesProxy) { 285 this.usesProxy = usesProxy; 286 } 78 public void setLoginProxy(String loginProxy); 287 79 288 80 /** 289 * @param urlProxy the urlProxy to set 81 * Setter of the proxy port 82 * @param port 290 83 */ 291 public void setUrlProxy(String urlProxy) { 292 this.urlProxy = urlProxy; 293 } 84 public void setPortProxy(int port); 294 85 295 86 /** 296 * @param portProxy the portProxy to set 87 * Setter of the proxy url 88 * @param urlProxy 297 89 */ 298 public void setPortProxy(int portProxy) { 299 this.portProxy = portProxy; 300 } 90 public void setUrlProxy(String urlProxy); 301 91 302 92 /** 303 * @param loginProxy the loginProxy to set 93 * Setter of the proxy pass 94 * @param proxyPass 304 95 */ 305 public void setLoginProxy(String loginProxy) { 306 this.loginProxy = loginProxy; 307 } 96 public void setPassProxy(String proxyPass); 308 97 309 98 /** 310 * @param passProxy the passProxy to set 99 * Setter of the proxy url 100 * @param url 311 101 */ 312 public void setPassProxy(String passProxy) { 313 this.passProxy = passProxy; 314 } 102 public void setUrl(String url); 315 103 316 public String getPassword() { 317 return password; 318 } 104 /** 105 * Setter of the boolean that tells to use a proxy or not 106 * @param usesProxy 107 */ 108 public void setUsesProxy(boolean usesProxy); 319 109 320 public void setPassword(String password) { 321 this.password = password; 322 } 323 324 public boolean isProxyError() { 325 return proxyError; 326 } 327 328 public void setProxyError(boolean proxyError) { 329 this.proxyError = proxyError; 330 } 331 110 /** 111 * Function that returns true if there is a proxy error 112 * @return 113 */ 114 public boolean isProxyError(); 332 115 } -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/transverse/session/impl/SessionManagerImpl.java
r9902 r9919 1 package fr.mael.jiwigo.transverse.session ;1 package fr.mael.jiwigo.transverse.session.impl; 2 2 3 3 import java.io.IOException; … … 17 17 import org.apache.http.conn.params.ConnRoutePNames; 18 18 import org.apache.http.impl.client.DefaultHttpClient; 19 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;20 19 import org.apache.http.message.BasicNameValuePair; 20 import org.slf4j.Logger; 21 import org.slf4j.LoggerFactory; 21 22 import org.w3c.dom.Document; 22 23 23 24 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; 24 25 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 26 import fr.mael.jiwigo.transverse.session.SessionManager; 25 27 import fr.mael.jiwigo.transverse.util.Tools; 26 28 … … 41 43 * Gestionnaire de connexion 42 44 */ 43 public class SessionManager {45 public class SessionManagerImpl implements SessionManager { 44 46 45 47 /** 46 48 * Logger 47 49 */ 48 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 49 .getLog(SessionManager.class); 50 private final Logger LOG = LoggerFactory.getLogger(SessionManagerImpl.class); 50 51 /** 51 52 * the entered login … … 94 95 */ 95 96 private boolean proxyError; 97 98 public SessionManagerImpl() { 99 client = new DefaultHttpClient(); 100 } 96 101 97 102 /** … … 100 105 * @param password the password 101 106 * @param url the url of the site 102 */ 103 public SessionManager(String login, String password, String url) { 107 * @param userAgent the user agent to use 108 */ 109 public SessionManagerImpl(String login, String password, String url, String userAgent) { 104 110 this.login = login; 105 111 this.password = password; … … 107 113 // MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); 108 114 // client = new HttpClient(connectionManager); 109 ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager();110 client = new DefaultHttpClient( connectionManager);115 // ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(null, null); 116 client = new DefaultHttpClient(); 111 117 //Using of a Linux user agent. cause... it's better 8) 112 client.getParams().setParameter("http.useragent", 113 "Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1"); 114 118 client.getParams().setParameter("http.useragent", userAgent); 119 120 } 121 122 /** 123 * Fonction used to set the user agent of the http client 124 * @param userAgent 125 */ 126 public void setUserAgent(String userAgent) { 127 client.getParams().setParameter("http.useragent", userAgent); 115 128 } 116 129 … … 181 194 } 182 195 method.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 183 //begin bug:0001833184 196 method.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); 185 //end186 197 try { 187 198 HttpResponse response = client.execute(method); … … 190 201 } 191 202 InputStream inputStream = response.getEntity().getContent(); 192 StringBuffer buffer = new StringBuffer(); 193 byte[] data = new byte[512]; 194 int len = 0; 195 try { 196 while (-1 != (len = inputStream.read(data))) { 197 buffer.append(new String(data, 0, len)); 198 } 199 } catch (IOException e) { 200 e.printStackTrace(); 201 } 202 try { 203 inputStream.close(); 204 } catch (IOException e) { 205 e.printStackTrace(); 206 } 207 return buffer.toString(); 203 String toReturn = Tools.readInputStreamAsString(inputStream); 204 return toReturn; 208 205 } catch (IOException e) { 209 206 -
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/transverse/util/Tools.java
r9902 r9919 40 40 import org.apache.sanselan.formats.jpeg.iptc.PhotoshopApp13Data; 41 41 import org.apache.sanselan.formats.tiff.write.TiffOutputSet; 42 import org.slf4j.Logger; 43 import org.slf4j.LoggerFactory; 42 44 import org.w3c.dom.Document; 43 45 import org.w3c.dom.Element; … … 46 48 import org.xml.sax.SAXException; 47 49 50 import fr.mael.jiwigo.service.impl.CategoryServiceImpl; 48 51 import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; 49 52 … … 68 71 * Logger 69 72 */ 70 p ublic static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(Tools.class);73 private static final Logger LOG = LoggerFactory.getLogger(CategoryServiceImpl.class); 71 74 72 75 /** … … 97 100 public static Document stringToDocument(String xmlSource) throws SAXException, ParserConfigurationException, 98 101 IOException { 102 LOG.debug(xmlSource); 99 103 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 100 104 DocumentBuilder builder = factory.newDocumentBuilder(); … … 327 331 } 328 332 333 public static Document readFileAsDocument(String filePath) { 334 Document doc = null; 335 try { 336 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); 337 DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); 338 doc = docBuilder.parse(filePath); 339 } catch (Exception e) { 340 LOG.error("Error converting file to Document : " + getStackTrace(e)); 341 } 342 return doc; 343 } 344 345 /** 346 * Gets the value of a document node 347 * @param element 348 * @param tagName 349 * @return 350 */ 329 351 public static String getStringValueDom(Element element, String tagName) { 330 352 Element el = (Element) element.getElementsByTagName(tagName).item(0); 331 353 return el.getFirstChild().getNodeValue(); 332 354 } 355 356 /** 357 * Sets the value of a document node 358 * @param element 359 * @param tagName 360 * @param value 361 */ 362 public static void setStringValueDom(Element element, String tagName, String value) { 363 Element el = (Element) element.getElementsByTagName(tagName).item(0); 364 el.setNodeValue(value); 365 } 366 367 /** 368 * Write an xml document to a file 369 * @param doc document to write 370 * @param filename file where to write the document 371 */ 372 public static void writeXmlFile(Document doc, String filename) { 373 try { 374 Source source = new DOMSource(doc); 375 File file = new File(filename); 376 Result result = new StreamResult(file); 377 Transformer xformer = TransformerFactory.newInstance().newTransformer(); 378 xformer.transform(source, result); 379 } catch (TransformerConfigurationException e) { 380 LOG.error(getStackTrace(e)); 381 } catch (TransformerException e) { 382 LOG.error(getStackTrace(e)); 383 } 384 } 333 385 }
Note: See TracChangeset
for help on using the changeset viewer.