Ignore:
Timestamp:
Apr 19, 2011, 3:36:26 AM (13 years ago)
Author:
anthony43
Message:

addSimple ws api method added, with unit tests
http://piwigo.org/doc/doku.php?id=en:dev:webapi:pwg.images.addsimple
pom upgraded to 0.3.0-SNAPSHOT since this is a minor change, and also a dependency was added (httpmime)
SessionManagerImpl was modified to make available its httpclient.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/ImageDaoImpl.java

    r9919 r10494  
    11package fr.mael.jiwigo.dao.impl;
    22
     3import java.io.BufferedReader;
    34import java.io.File;
    45import java.io.IOException;
     6import java.io.InputStreamReader;
     7import java.net.HttpURLConnection;
    58import java.security.NoSuchAlgorithmException;
    69import java.util.ArrayList;
     
    811import java.util.List;
    912
     13import org.apache.http.HttpEntity;
     14import org.apache.http.HttpResponse;
     15import org.apache.http.client.methods.HttpPost;
     16import org.apache.http.entity.BufferedHttpEntity;
     17import org.apache.http.entity.mime.MultipartEntity;
     18import org.apache.http.entity.mime.content.FileBody;
     19import org.apache.http.entity.mime.content.StringBody;
    1020import org.slf4j.Logger;
    1121import org.slf4j.LoggerFactory;
     
    2333import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException;
    2434import fr.mael.jiwigo.transverse.session.SessionManager;
     35import fr.mael.jiwigo.transverse.session.impl.SessionManagerImpl;
    2536import fr.mael.jiwigo.transverse.util.Tools;
    2637
     
    89100            Document doc = null;
    90101            if (categoryId != null) {
    91                 doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel(), "cat_id", String
    92                         .valueOf(categoryId));
     102                doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel(), "cat_id",
     103                        String.valueOf(categoryId));
    93104            } else {
    94105                doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel());
     
    133144        //sends the thumbnail and gets the result
    134145        Document reponseThumb = (sessionManager.executeReturnDocument("pwg.images.addChunk", "data", thumbnailBase64,
    135                 "type", "thumb", "position", "1", "original_sum", Tools.getMD5Checksum(image.getOriginale()
    136                         .getAbsolutePath())));
     146                "type", "thumb", "position", "1", "original_sum",
     147                Tools.getMD5Checksum(image.getOriginale().getAbsolutePath())));
    137148
    138149        //begin feature:0001827
     
    147158            String originaleBase64 = base64.encode(Tools.getBytesFromFile(fichierAEnvoyer));
    148159            Document reponseOriginale = (sessionManager.executeReturnDocument("pwg.images.addChunk", "data",
    149                     originaleBase64, "type", "file", "position", String.valueOf(i), "original_sum", Tools
    150                             .getMD5Checksum(image.getOriginale().getAbsolutePath())));
     160                    originaleBase64, "type", "file", "position", String.valueOf(i), "original_sum",
     161                    Tools.getMD5Checksum(image.getOriginale().getAbsolutePath())));
    151162            if (!Tools.checkOk(reponseOriginale)) {
    152163                echec = true;
     
    157168
    158169        //add the image in the database and get the result of the webservice
    159         Document reponseAjout = (sessionManager.executeReturnDocument("pwg.images.add", "file_sum", Tools
    160                 .getMD5Checksum(image.getOriginale().getAbsolutePath()), "thumbnail_sum", Tools.getMD5Checksum(image
    161                 .getThumbnail().getCanonicalPath()), "position", "1", "original_sum", Tools.getMD5Checksum(image
    162                 .getOriginale().getAbsolutePath()), "categories", String.valueOf(image.getIdCategory()), "name", image
    163                 .getName(), "author", sessionManager.getLogin(), "level", image.getPrivacyLevel()));
     170        Document reponseAjout = (sessionManager.executeReturnDocument("pwg.images.add", "file_sum",
     171                Tools.getMD5Checksum(image.getOriginale().getAbsolutePath()), "thumbnail_sum",
     172                Tools.getMD5Checksum(image.getThumbnail().getCanonicalPath()), "position", "1", "original_sum",
     173                Tools.getMD5Checksum(image.getOriginale().getAbsolutePath()), "categories",
     174                String.valueOf(image.getIdCategory()), "name", image.getName(), "author", sessionManager.getLogin(),
     175                "level", image.getPrivacyLevel()));
    164176        LOG.debug("Response add : " + Tools.documentToString(reponseAjout));
    165177        //      System.out.println(Main.sessionManager.executerReturnString("pwg.images.add", "file_sum", Outil
     
    189201     */
    190202    public boolean addTags(Integer imageId, String tagId) throws IOException, ProxyAuthenticationException {
    191         Document doc = sessionManager.executeReturnDocument(MethodsEnum.SET_INFO.getLabel(), "image_id", String
    192                 .valueOf(imageId), "tag_ids", tagId);
     203        Document doc = sessionManager.executeReturnDocument(MethodsEnum.SET_INFO.getLabel(), "image_id",
     204                String.valueOf(imageId), "tag_ids", tagId);
    193205        try {
    194206            return Tools.checkOk(doc);
     
    260272    }
    261273
     274    public void addSimple(File file, Integer category, String title) throws IOException {
     275        HttpPost httpMethod = new HttpPost(((SessionManagerImpl) sessionManager).getUrl());
     276
     277        //      nameValuePairs.add(new BasicNameValuePair("method", "pwg.images.addSimple"));
     278        //      for (int i = 0; i < parametres.length; i += 2) {
     279        //          nameValuePairs.add(new BasicNameValuePair(parametres[i], parametres[i + 1]));
     280        //      }
     281        //      method.setEntity(new UrlEncodedFormEntity(nameValuePairs));
     282
     283        if (file != null) {
     284            MultipartEntity multipartEntity = new MultipartEntity();
     285
     286            //          String string = nameValuePairs.toString();
     287            // dirty fix to remove the enclosing entity{}
     288            //          String substring = string.substring(string.indexOf("{"),
     289            //                          string.lastIndexOf("}") + 1);
     290            multipartEntity.addPart("method", new StringBody("pwg.images.addSimple"));
     291            multipartEntity.addPart("category", new StringBody(category.toString()));
     292            multipartEntity.addPart("name", new StringBody(title));
     293
     294            //          StringBody contentBody = new StringBody(substring,
     295            //                          Charset.forName("UTF-8"));
     296            //          multipartEntity.addPart("entity", contentBody);
     297            FileBody fileBody = new FileBody(file);
     298            multipartEntity.addPart("image", fileBody);
     299            ((HttpPost) httpMethod).setEntity(multipartEntity);
     300        }
     301
     302        //      httpMethod.setHeader(X_GALLERY_REQUEST_METHOD, requestMethod);
     303        HttpResponse response = ((SessionManagerImpl) sessionManager).getClient().execute(httpMethod);
     304        int responseStatusCode = response.getStatusLine().getStatusCode();
     305
     306        switch (responseStatusCode) {
     307        case HttpURLConnection.HTTP_CREATED:
     308            break;
     309        case HttpURLConnection.HTTP_OK:
     310            break;
     311        case HttpURLConnection.HTTP_BAD_REQUEST:
     312            throw new IOException("status code was : " + responseStatusCode);
     313        case HttpURLConnection.HTTP_FORBIDDEN:
     314            throw new IOException("status code was : " + responseStatusCode);
     315        case HttpURLConnection.HTTP_NOT_FOUND:
     316            throw new IOException("status code was : " + responseStatusCode);
     317        default:
     318            throw new IOException("status code was : " + responseStatusCode);
     319        }
     320
     321        HttpEntity resultEntity = response.getEntity();
     322        BufferedHttpEntity responseEntity = new BufferedHttpEntity(resultEntity);
     323
     324        BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
     325        String line;
     326        StringBuilder sb = new StringBuilder();
     327        try {
     328            while ((line = reader.readLine()) != null) {
     329                sb.append(line);
     330                sb.append("\n");
     331            }
     332        } finally {
     333            reader.close();
     334        }
     335        String stringResult = sb.toString();
     336
     337    }
     338
    262339    public SessionManager getSessionManager() {
    263340        return sessionManager;
     
    267344        this.sessionManager = sessionManager;
    268345    }
    269 
    270346}
Note: See TracChangeset for help on using the changeset viewer.