Changeset 9919


Ignore:
Timestamp:
Mar 29, 2011, 8:21:25 PM (10 years ago)
Author:
mlg
Message:

Complete rewriting of the api
The api now uses interfaces everywhere (for the dao, services and session manager). This allows the developer to use his own implementations if he wants.
Deletion of the singletons. There are now getters and setters. It allows to make dependency injection.
Use of sl4j in the api, instead of using log4j.

Location:
extensions/jiwigo-ws-api
Files:
3 added
1 deleted
12 edited
9 copied

Legend:

Unmodified
Added
Removed
  • extensions/jiwigo-ws-api/.classpath

    r9902 r9919  
    55  <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
    66  <classpathentry kind="output" path="target/classes"/>
    7   <classpathentry kind="var" path="M2_REPO/javax/servlet/servlet-api/2.3/servlet-api-2.3.jar"/>
    8   <classpathentry kind="var" path="M2_REPO/avalon-framework/avalon-framework/4.1.3/avalon-framework-4.1.3.jar"/>
    9   <classpathentry kind="var" path="M2_REPO/commons-beanutils/commons-beanutils/1.8.3/commons-beanutils-1.8.3.jar"/>
    107  <classpathentry kind="var" path="M2_REPO/commons-codec/commons-codec/1.4/commons-codec-1.4.jar"/>
    118  <classpathentry kind="var" path="M2_REPO/commons-lang/commons-lang/2.5/commons-lang-2.5.jar"/>
    12   <classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1/commons-logging-1.1.jar"/>
     9  <classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar"/>
    1310  <classpathentry kind="var" path="M2_REPO/org/apache/httpcomponents/httpclient/4.1/httpclient-4.1.jar"/>
    1411  <classpathentry kind="var" path="M2_REPO/org/apache/httpcomponents/httpcore/4.1/httpcore-4.1.jar"/>
    15   <classpathentry kind="var" path="M2_REPO/junit/junit/4.8.1/junit-4.8.1.jar"/>
    16   <classpathentry kind="var" path="M2_REPO/log4j/log4j/1.2.16/log4j-1.2.16.jar"/>
    17   <classpathentry kind="var" path="M2_REPO/logkit/logkit/1.0.1/logkit-1.0.1.jar"/>
    18   <classpathentry kind="var" path="M2_REPO/com/drew/metadata-extractor/2.3.1/metadata-extractor-2.3.1.jar"/>
    1912  <classpathentry kind="var" path="M2_REPO/org/apache/sanselan/sanselan/0.97-incubator/sanselan-0.97-incubator.jar"/>
     13  <classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar"/>
    2014  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/java-6-openjdk"/>
    2115</classpath>
  • extensions/jiwigo-ws-api/.project

    r9902 r9919  
    1 <?xml version="1.0" encoding="UTF-8"?>
    21<projectDescription>
    3         <name>jiwigo-ws-api</name>
    4         <comment>NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
    5         <projects>
    6         </projects>
    7         <buildSpec>
    8                 <buildCommand>
    9                         <name>org.eclipse.jdt.core.javabuilder</name>
    10                         <arguments>
    11                         </arguments>
    12                 </buildCommand>
    13                 <buildCommand>
    14                         <name>org.maven.ide.eclipse.maven2Builder</name>
    15                         <arguments>
    16                         </arguments>
    17                 </buildCommand>
    18         </buildSpec>
    19         <natures>
    20                 <nature>org.maven.ide.eclipse.maven2Nature</nature>
    21                 <nature>org.eclipse.jdt.core.javanature</nature>
    22         </natures>
     2  <name>jiwigo-ws-api</name>
     3  <comment>NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
     4  <projects/>
     5  <buildSpec>
     6    <buildCommand>
     7      <name>org.eclipse.jdt.core.javabuilder</name>
     8    </buildCommand>
     9    <buildCommand>
     10      <name>org.maven.ide.eclipse.maven2Builder</name>
     11    </buildCommand>
     12  </buildSpec>
     13  <natures>
     14    <nature>org.maven.ide.eclipse.maven2Nature</nature>
     15    <nature>org.eclipse.jdt.core.javanature</nature>
     16  </natures>
    2317</projectDescription>
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/CategoryDao.java

    r9902 r9919  
    22
    33import java.io.IOException;
    4 import java.util.ArrayList;
    54import java.util.List;
    65
    7 import org.w3c.dom.Document;
    8 import org.w3c.dom.Element;
    9 import org.w3c.dom.Node;
    10 import org.w3c.dom.NodeList;
     6import fr.mael.jiwigo.om.Category;
     7import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    118
    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     }
     9public interface CategoryDao {
    6510
    6611    /**
     
    7116     * @throws ProxyAuthenticationException
    7217     */
    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;
    10319
    10420    /**
     
    10824     * @throws ProxyAuthenticationException
    10925     */
    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;
    12727}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/CommentDao.java

    r9902 r9919  
    22
    33import java.io.IOException;
    4 import java.util.ArrayList;
    54import java.util.List;
    65
    7 import org.w3c.dom.Document;
    8 import org.w3c.dom.Element;
    9 import org.w3c.dom.Node;
    10 import org.w3c.dom.NodeList;
     6import fr.mael.jiwigo.om.Comment;
     7import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    118
    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 
     9public interface CommentDao {
    6610    /**
    6711     * Listing of the comments for the given image
     
    7115     * @throws ProxyAuthenticationException
    7216     */
    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;
    9418
    9519    /**
     
    10024     * @throws ProxyAuthenticationException
    10125     */
    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;
    11027
    11128    /**
     
    11633     * @throws ProxyAuthenticationException
    11734     */
    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;
    13536}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/ImageDao.java

    r9902 r9919  
    11package fr.mael.jiwigo.dao;
    22
    3 import java.io.File;
    43import java.io.IOException;
    54import java.security.NoSuchAlgorithmException;
    6 import java.util.ArrayList;
    7 import java.util.HashMap;
    85import java.util.List;
    96
    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;
    167import fr.mael.jiwigo.om.Image;
    17 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
    188import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
    199import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    2010import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException;
    21 import fr.mael.jiwigo.transverse.session.SessionManager;
    22 import fr.mael.jiwigo.transverse.util.Tools;
    2311
    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 
     12public interface ImageDao {
    8613    /**
    8714     * Lists all images
     
    9017     * @throws ProxyAuthenticationException
    9118     */
    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;
    9520
    9621    /**
     
    10227     */
    10328    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;
    12530
    12631    /**
     
    14348    //TODO ne pas continuer si une des reponses precedentes est negative
    14449    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;
    19951
    20052    /**
     
    20557     * @throws ProxyAuthenticationException
    20658     */
    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;
    25260
    25361    /**
     
    25866     * @throws ProxyAuthenticationException
    25967     */
    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;
    27869
    27970}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/TagDao.java

    r9902 r9919  
    22
    33import java.io.IOException;
    4 import java.util.ArrayList;
    54import 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;
    115
    126import fr.mael.jiwigo.om.Image;
    137import 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;
    178import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    18 import fr.mael.jiwigo.transverse.session.SessionManager;
    19 import fr.mael.jiwigo.transverse.util.Tools;
    209
    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 
     10public interface TagDao {
    6611    /**
    6712     * lists the tags
     
    7015     * @throws ProxyAuthenticationException
    7116     */
    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;
    10318
    10419    /**
     
    10823     * @throws ProxyAuthenticationException
    10924     */
    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;
    12126
    12227    /**
     
    12732     * @throws ProxyAuthenticationException
    12833     */
    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;
    13735}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/CategoryDaoImpl.java

    r9902 r9919  
    1 package fr.mael.jiwigo.dao;
     1package fr.mael.jiwigo.dao.impl;
    22
    33import java.io.IOException;
     
    55import java.util.List;
    66
     7import org.slf4j.Logger;
     8import org.slf4j.LoggerFactory;
    79import org.w3c.dom.Document;
    810import org.w3c.dom.Element;
     
    1012import org.w3c.dom.NodeList;
    1113
     14import fr.mael.jiwigo.dao.CategoryDao;
    1215import fr.mael.jiwigo.om.Category;
    1316import fr.mael.jiwigo.transverse.enumeration.CategoryEnum;
     
    3437 *
    3538 */
    36 public class CategoryDao {
     39public class CategoryDaoImpl implements CategoryDao {
    3740    /**
    3841     * Logger
    3942     */
    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);
    4244    /**
    4345     * Instance to use a singleton
    4446     */
    45     private static CategoryDao instance;
     47    private static CategoryDaoImpl instance;
    4648
    4749    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     }
    6550
    6651    /**
     
    125110    }
    126111
     112    public SessionManager getSessionManager() {
     113        return sessionManager;
     114    }
     115
     116    public void setSessionManager(SessionManager sessionManager) {
     117        this.sessionManager = sessionManager;
     118    }
     119
    127120}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/CommentDaoImpl.java

    r9902 r9919  
    1 package fr.mael.jiwigo.dao;
     1package fr.mael.jiwigo.dao.impl;
    22
    33import java.io.IOException;
     
    55import java.util.List;
    66
     7import org.slf4j.Logger;
     8import org.slf4j.LoggerFactory;
    79import org.w3c.dom.Document;
    810import org.w3c.dom.Element;
     
    1012import org.w3c.dom.NodeList;
    1113
     14import fr.mael.jiwigo.dao.CommentDao;
    1215import fr.mael.jiwigo.om.Comment;
    1316import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
     
    3437 *
    3538 */
    36 public class CommentDao {
     39public class CommentDaoImpl implements CommentDao {
    3740    /**
    3841     * Logger
    3942     */
    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);
    4644
    4745    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     }
    6546
    6647    /**
     
    133114
    134115    }
     116
     117    public SessionManager getSessionManager() {
     118        return sessionManager;
     119    }
     120
     121    public void setSessionManager(SessionManager sessionManager) {
     122        this.sessionManager = sessionManager;
     123    }
     124
    135125}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/ImageDaoImpl.java

    r9902 r9919  
    1 package fr.mael.jiwigo.dao;
     1package fr.mael.jiwigo.dao.impl;
    22
    33import java.io.File;
     
    88import java.util.List;
    99
     10import org.slf4j.Logger;
     11import org.slf4j.LoggerFactory;
    1012import org.w3c.dom.Document;
    1113import org.w3c.dom.Element;
     
    1416
    1517import sun.misc.BASE64Encoder;
     18import fr.mael.jiwigo.dao.ImageDao;
    1619import fr.mael.jiwigo.om.Image;
    1720import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
     
    3942 *
    4043 */
    41 public class ImageDao {
     44public class ImageDaoImpl implements ImageDao {
    4245
    4346    /**
    4447     * Logger
    4548     */
    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);
    5350
    5451    /**
     
    6663    private ArrayList<File> filesToSend;
    6764
    68     /**
    69      * Private singleton, to use a singleton
    70      */
    71     private ImageDao(SessionManager sessionManager) {
     65    public ImageDaoImpl() {
    7266        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;
    8467    }
    8568
     
    260243    public List<Image> search(String searchString) throws IOException, ProxyAuthenticationException {
    261244        Document doc = sessionManager.executeReturnDocument(MethodsEnum.SEARCH.getLabel(), "query", searchString);
    262         LOG.debug(doc);
     245        LOG.debug(Tools.documentToString(doc));
    263246        Element element = (Element) doc.getDocumentElement().getElementsByTagName("images").item(0);
    264247        return getImagesFromElement(element);
     
    277260    }
    278261
     262    public SessionManager getSessionManager() {
     263        return sessionManager;
     264    }
     265
     266    public void setSessionManager(SessionManager sessionManager) {
     267        this.sessionManager = sessionManager;
     268    }
     269
    279270}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/TagDaoImpl.java

    r9902 r9919  
    1 package fr.mael.jiwigo.dao;
     1package fr.mael.jiwigo.dao.impl;
    22
    33import java.io.IOException;
     
    55import java.util.List;
    66
     7import org.slf4j.Logger;
     8import org.slf4j.LoggerFactory;
    79import org.w3c.dom.Document;
    810import org.w3c.dom.Element;
     
    1012import org.w3c.dom.NodeList;
    1113
     14import fr.mael.jiwigo.dao.TagDao;
    1215import fr.mael.jiwigo.om.Image;
    1316import fr.mael.jiwigo.om.Tag;
     
    3538 *
    3639 */
    37 public class TagDao {
     40public class TagDaoImpl implements TagDao {
    3841    /**
    3942     * Logger
    4043     */
    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);
    4645
    4746    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     }
    6547
    6648    /**
     
    135117    }
    136118
     119    public SessionManager getSessionManager() {
     120        return sessionManager;
     121    }
     122
     123    public void setSessionManager(SessionManager sessionManager) {
     124        this.sessionManager = sessionManager;
     125    }
     126
    137127}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/CategoryService.java

    r9879 r9919  
    44import java.util.List;
    55
    6 import fr.mael.jiwigo.dao.CategoryDao;
    76import fr.mael.jiwigo.om.Category;
    87import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    9 import fr.mael.jiwigo.transverse.session.SessionManager;
    108
    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 
     9public interface CategoryService {
    5910    /**
    6011     * Lists all categories
     
    6415     * @throws ProxyAuthenticationException
    6516     */
    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;
    6918
    7019    /**
     
    7423     * @throws ProxyAuthenticationException
    7524     */
    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;
    9326
    9427    /**
     
    9932     * @throws ProxyAuthenticationException
    10033     */
    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;
    10735
    10836    /**
     
    11240     * @throws ProxyAuthenticationException
    11341     */
    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;
    11943}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/CommentService.java

    r9879 r9919  
    44import java.util.List;
    55
    6 import fr.mael.jiwigo.dao.CommentDao;
    76import fr.mael.jiwigo.om.Comment;
    87import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    9 import fr.mael.jiwigo.transverse.session.SessionManager;
    108
    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 
     9public interface CommentService {
    5810    /**
    5911     * Lists all comments for an image
     
    6315     * @throws ProxyAuthenticationException
    6416     */
    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;
    6818
    6919    /**
     
    7727     */
    7828    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;
    8630
    8731}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/ImageService.java

    r9893 r9919  
    11package fr.mael.jiwigo.service;
    22
    3 import java.io.File;
    43import java.io.IOException;
    54import java.security.NoSuchAlgorithmException;
    65import java.util.List;
    76
    8 import fr.mael.jiwigo.dao.ImageDao;
    97import fr.mael.jiwigo.om.Image;
    108import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
    119import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    1210import 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;
    1611
    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     }
     12public interface ImageService {
    6513
    6614    /**
     
    7220     */
    7321    public List<Image> listByCategory(Integer categoryId, boolean rafraichir) throws IOException,
    74             ProxyAuthenticationException {
    75         return ImageDao.getInstance(sessionManager).listByCategory(categoryId, rafraichir);
    76     }
     22            ProxyAuthenticationException;
    7723
    7824    /**
     
    9238    public boolean create(String filePath, Integer idCategory, Integer originalWidth, Integer originalHeight,
    9339            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;
    13141
    13242    /**
     
    13848     * @throws ProxyAuthenticationException
    13949     */
    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;
    14351
    14452    /**
     
    14957     * @throws ProxyAuthenticationException
    15058     */
    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;
    16660
    16761}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/TagService.java

    r9879 r9919  
    44import java.util.List;
    55
    6 import fr.mael.jiwigo.dao.TagDao;
    76import fr.mael.jiwigo.om.Image;
    87import fr.mael.jiwigo.om.Tag;
    98import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    10 import fr.mael.jiwigo.transverse.session.SessionManager;
    119
    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     }
     10public interface TagService {
    5911
    6012    /**
     
    6416     * @throws ProxyAuthenticationException
    6517     */
    66     public List<Tag> list() throws IOException, ProxyAuthenticationException {
    67         return TagDao.getInstance(sessionManager).list();
    68     }
     18    public List<Tag> list() throws IOException, ProxyAuthenticationException;
    6919
    7020    /**
     
    7525     * @throws ProxyAuthenticationException
    7626     */
    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;
    8228
    8329    /**
     
    8834     * @throws ProxyAuthenticationException
    8935     */
    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;
    9437}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/impl/CategoryServiceImpl.java

    r9879 r9919  
    1 package fr.mael.jiwigo.service;
     1package fr.mael.jiwigo.service.impl;
    22
    33import java.io.IOException;
    44import java.util.List;
    55
     6import org.slf4j.Logger;
     7import org.slf4j.LoggerFactory;
     8
    69import fr.mael.jiwigo.dao.CategoryDao;
    710import fr.mael.jiwigo.om.Category;
     11import fr.mael.jiwigo.service.CategoryService;
    812import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    9 import fr.mael.jiwigo.transverse.session.SessionManager;
    1013
    1114/*
     
    2528 *
    2629 */
    27 public class CategoryService {
     30public class CategoryServiceImpl implements CategoryService {
    2831    /**
    2932     * Logger
    3033     */
    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);
    3335
    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;
    5837
    5938    /**
     
    6544     */
    6645    public List<Category> list(boolean recursive) throws IOException, ProxyAuthenticationException {
    67         return CategoryDao.getInstance(sessionManager).list(recursive);
     46        return dao.list(recursive);
    6847    }
    6948
     
    7554     */
    7655    public List<Category> makeTree() throws IOException, ProxyAuthenticationException {
    77         List<Category> list = CategoryDao.getInstance(sessionManager).list(true);
     56        List<Category> list = dao.list(true);
    7857        for (Category category : list) {
    7958            for (Category category2 : list) {
     
    10382        category.setDirectParent(parent);
    10483        category.setName(nom);
    105         return CategoryDao.getInstance(sessionManager).create(category);
     84        return dao.create(category);
    10685    }
    10786
     
    11594        Category category = new Category();
    11695        category.setName(nom);
    117         return CategoryDao.getInstance(sessionManager).create(category);
     96        return dao.create(category);
    11897    }
     98
     99    public CategoryDao getDao() {
     100        return dao;
     101    }
     102
     103    public void setDao(CategoryDao dao) {
     104        this.dao = dao;
     105    }
     106
    119107}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/impl/CommentServiceImpl.java

    r9879 r9919  
    1 package fr.mael.jiwigo.service;
     1package fr.mael.jiwigo.service.impl;
    22
    33import java.io.IOException;
    44import java.util.List;
    55
     6import org.slf4j.Logger;
     7import org.slf4j.LoggerFactory;
     8
    69import fr.mael.jiwigo.dao.CommentDao;
    710import fr.mael.jiwigo.om.Comment;
     11import fr.mael.jiwigo.service.CommentService;
    812import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    9 import fr.mael.jiwigo.transverse.session.SessionManager;
    1013
    1114/*
     
    2528 *
    2629 */
    27 public class CommentService {
     30public class CommentServiceImpl implements CommentService {
    2831    /**
    2932     * Logger
    3033     */
    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);
    3335
    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;
    5737
    5838    /**
     
    6444     */
    6545    public List<Comment> list(Integer imageId) throws IOException, ProxyAuthenticationException {
    66         return CommentDao.getInstance(sessionManager).list(imageId);
     46        return dao.list(imageId);
    6747    }
    6848
     
    8262        comment.setImageId(imageId);
    8363        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;
    8573    }
    8674
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/impl/ImageServiceImpl.java

    r9893 r9919  
    1 package fr.mael.jiwigo.service;
     1package fr.mael.jiwigo.service.impl;
    22
    33import java.io.File;
     
    66import java.util.List;
    77
     8import org.slf4j.Logger;
     9import org.slf4j.LoggerFactory;
     10
    811import fr.mael.jiwigo.dao.ImageDao;
    912import fr.mael.jiwigo.om.Image;
     13import fr.mael.jiwigo.service.ImageService;
    1014import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
    1115import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    1216import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException;
    13 import fr.mael.jiwigo.transverse.session.SessionManager;
    1417import fr.mael.jiwigo.transverse.util.ImagesUtil;
    1518import fr.mael.jiwigo.transverse.util.Tools;
     
    3235 *
    3336 */
    34 public class ImageService {
     37public class ImageServiceImpl implements ImageService {
    3538
    3639    /**
    3740     * Logger
    3841     */
    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);
    4143
    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;
    6545
    6646    /**
     
    7353    public List<Image> listByCategory(Integer categoryId, boolean rafraichir) throws IOException,
    7454            ProxyAuthenticationException {
    75         return ImageDao.getInstance(sessionManager).listByCategory(categoryId, rafraichir);
     55        return dao.listByCategory(categoryId, rafraichir);
    7656    }
    7757
     
    127107        image.setPrivacyLevel(String.valueOf(privacyLevel));
    128108        //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);
    130110    }
    131111
     
    139119     */
    140120    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);
    142122    }
    143123
     
    150130     */
    151131    public List<Image> search(String queryString) throws IOException, ProxyAuthenticationException {
    152         return ImageDao.getInstance(sessionManager).search(queryString);
     132        return dao.search(queryString);
    153133    }
    154134
     
    165145    }
    166146
     147    public ImageDao getDao() {
     148        return dao;
     149    }
     150
     151    public void setDao(ImageDao dao) {
     152        this.dao = dao;
     153    }
     154
    167155}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/impl/TagServiceImpl.java

    r9879 r9919  
    1 package fr.mael.jiwigo.service;
     1package fr.mael.jiwigo.service.impl;
    22
    33import java.io.IOException;
    44import java.util.List;
    55
     6import org.slf4j.Logger;
     7import org.slf4j.LoggerFactory;
     8
    69import fr.mael.jiwigo.dao.TagDao;
    710import fr.mael.jiwigo.om.Image;
    811import fr.mael.jiwigo.om.Tag;
     12import fr.mael.jiwigo.service.TagService;
    913import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    10 import fr.mael.jiwigo.transverse.session.SessionManager;
    1114
    1215/*
     
    2730 *
    2831 */
    29 public class TagService {
     32public class TagServiceImpl implements TagService {
    3033    /**
    3134     * Logger
    3235     */
    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);
    3537
    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;
    5939
    6040    /**
     
    6545     */
    6646    public List<Tag> list() throws IOException, ProxyAuthenticationException {
    67         return TagDao.getInstance(sessionManager).list();
     47        return dao.list();
    6848    }
    6949
     
    7858        Tag tag = new Tag();
    7959        tag.setName(nom);
    80         return TagDao.getInstance(sessionManager).create(tag);
     60        return dao.create(tag);
    8161    }
    8262
     
    8969     */
    9070    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;
    9280    }
    9381
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/transverse/session/SessionManager.java

    r9902 r9919  
    22
    33import java.io.IOException;
    4 import java.io.InputStream;
    54import java.io.UnsupportedEncodingException;
    6 import java.util.ArrayList;
    7 import java.util.List;
    85
    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;
    216import org.w3c.dom.Document;
    227
    23 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
    248import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    25 import fr.mael.jiwigo.transverse.util.Tools;
    269
    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 
     10public interface SessionManager {
    11711    /**
    11812     * Connection method
     
    12216     *
    12317     */
    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();
    15719
    15820    /**
     
    16527     */
    16628    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;
    21430
    21531    /**
     
    22238     */
    22339    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;
    23541
    23642    /**
     
    24046     * @throws ProxyAuthenticationException
    24147     */
    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;
    25249
    25350    /**
     51     * Getter of the login
    25452     * @return the login
    25553     */
    256     public String getLogin() {
    257         return login;
    258     }
     54    public String getLogin();
    25955
    26056    /**
    261      * @param login the login to set
     57     * Setter of the login
     58     * @param login
    26259     */
    263     public void setLogin(String login) {
    264         this.login = login;
    265     }
     60    public void setLogin(String login);
    26661
    26762    /**
    268      * @return the url
     63     * Setter of the user agent
     64     * @param userAgent
    26965     */
    270     public String getUrl() {
    271         return url;
    272     }
     66    public void setUserAgent(String userAgent);
    27367
    27468    /**
    275      * @param url the url to set
     69     * Setter of the password to access piwigo
     70     * @param password
    27671     */
    277     public void setUrl(String url) {
    278         this.url = url;
    279     }
     72    public void setPassword(String password);
    28073
    28174    /**
    282      * @param usesProxy the usesProxy to set
     75     * Setter of the proxy login
     76     * @param loginProxy
    28377     */
    284     public void setUsesProxy(boolean usesProxy) {
    285         this.usesProxy = usesProxy;
    286     }
     78    public void setLoginProxy(String loginProxy);
    28779
    28880    /**
    289      * @param urlProxy the urlProxy to set
     81     * Setter of the proxy port
     82     * @param port
    29083     */
    291     public void setUrlProxy(String urlProxy) {
    292         this.urlProxy = urlProxy;
    293     }
     84    public void setPortProxy(int port);
    29485
    29586    /**
    296      * @param portProxy the portProxy to set
     87     * Setter of the proxy url
     88     * @param urlProxy
    29789     */
    298     public void setPortProxy(int portProxy) {
    299         this.portProxy = portProxy;
    300     }
     90    public void setUrlProxy(String urlProxy);
    30191
    30292    /**
    303      * @param loginProxy the loginProxy to set
     93     * Setter of the proxy pass
     94     * @param proxyPass
    30495     */
    305     public void setLoginProxy(String loginProxy) {
    306         this.loginProxy = loginProxy;
    307     }
     96    public void setPassProxy(String proxyPass);
    30897
    30998    /**
    310      * @param passProxy the passProxy to set
     99     * Setter of the proxy url
     100     * @param url
    311101     */
    312     public void setPassProxy(String passProxy) {
    313         this.passProxy = passProxy;
    314     }
     102    public void setUrl(String url);
    315103
    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);
    319109
    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();
    332115}
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/transverse/session/impl/SessionManagerImpl.java

    r9902 r9919  
    1 package fr.mael.jiwigo.transverse.session;
     1package fr.mael.jiwigo.transverse.session.impl;
    22
    33import java.io.IOException;
     
    1717import org.apache.http.conn.params.ConnRoutePNames;
    1818import org.apache.http.impl.client.DefaultHttpClient;
    19 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
    2019import org.apache.http.message.BasicNameValuePair;
     20import org.slf4j.Logger;
     21import org.slf4j.LoggerFactory;
    2122import org.w3c.dom.Document;
    2223
    2324import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
    2425import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
     26import fr.mael.jiwigo.transverse.session.SessionManager;
    2527import fr.mael.jiwigo.transverse.util.Tools;
    2628
     
    4143 * Gestionnaire de connexion
    4244 */
    43 public class SessionManager {
     45public class SessionManagerImpl implements SessionManager {
    4446
    4547    /**
    4648     * Logger
    4749     */
    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);
    5051    /**
    5152     * the entered login
     
    9495     */
    9596    private boolean proxyError;
     97
     98    public SessionManagerImpl() {
     99        client = new DefaultHttpClient();
     100    }
    96101
    97102    /**
     
    100105     * @param password the password
    101106     * @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) {
    104110        this.login = login;
    105111        this.password = password;
     
    107113        //      MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    108114        //      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();
    111117        //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);
    115128    }
    116129
     
    181194        }
    182195        method.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    183         //begin bug:0001833
    184196        method.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    185         //end
    186197        try {
    187198            HttpResponse response = client.execute(method);
     
    190201            }
    191202            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;
    208205        } catch (IOException e) {
    209206
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/transverse/util/Tools.java

    r9902 r9919  
    4040import org.apache.sanselan.formats.jpeg.iptc.PhotoshopApp13Data;
    4141import org.apache.sanselan.formats.tiff.write.TiffOutputSet;
     42import org.slf4j.Logger;
     43import org.slf4j.LoggerFactory;
    4244import org.w3c.dom.Document;
    4345import org.w3c.dom.Element;
     
    4648import org.xml.sax.SAXException;
    4749
     50import fr.mael.jiwigo.service.impl.CategoryServiceImpl;
    4851import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
    4952
     
    6871     * Logger
    6972     */
    70     public 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);
    7174
    7275    /**
     
    97100    public static Document stringToDocument(String xmlSource) throws SAXException, ParserConfigurationException,
    98101            IOException {
     102        LOG.debug(xmlSource);
    99103        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    100104        DocumentBuilder builder = factory.newDocumentBuilder();
     
    327331    }
    328332
     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     */
    329351    public static String getStringValueDom(Element element, String tagName) {
    330352        Element el = (Element) element.getElementsByTagName(tagName).item(0);
    331353        return el.getFirstChild().getNodeValue();
    332354    }
     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    }
    333385}
Note: See TracChangeset for help on using the changeset viewer.