Changeset 10494 for extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/ImageDaoImpl.java
- Timestamp:
- Apr 19, 2011, 3:36:26 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/ImageDaoImpl.java
r9919 r10494 1 1 package fr.mael.jiwigo.dao.impl; 2 2 3 import java.io.BufferedReader; 3 4 import java.io.File; 4 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.net.HttpURLConnection; 5 8 import java.security.NoSuchAlgorithmException; 6 9 import java.util.ArrayList; … … 8 11 import java.util.List; 9 12 13 import org.apache.http.HttpEntity; 14 import org.apache.http.HttpResponse; 15 import org.apache.http.client.methods.HttpPost; 16 import org.apache.http.entity.BufferedHttpEntity; 17 import org.apache.http.entity.mime.MultipartEntity; 18 import org.apache.http.entity.mime.content.FileBody; 19 import org.apache.http.entity.mime.content.StringBody; 10 20 import org.slf4j.Logger; 11 21 import org.slf4j.LoggerFactory; … … 23 33 import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException; 24 34 import fr.mael.jiwigo.transverse.session.SessionManager; 35 import fr.mael.jiwigo.transverse.session.impl.SessionManagerImpl; 25 36 import fr.mael.jiwigo.transverse.util.Tools; 26 37 … … 89 100 Document doc = null; 90 101 if (categoryId != null) { 91 doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel(), "cat_id", String92 .valueOf(categoryId));102 doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel(), "cat_id", 103 String.valueOf(categoryId)); 93 104 } else { 94 105 doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel()); … … 133 144 //sends the thumbnail and gets the result 134 145 Document reponseThumb = (sessionManager.executeReturnDocument("pwg.images.addChunk", "data", thumbnailBase64, 135 "type", "thumb", "position", "1", "original_sum", Tools.getMD5Checksum(image.getOriginale()136 146 "type", "thumb", "position", "1", "original_sum", 147 Tools.getMD5Checksum(image.getOriginale().getAbsolutePath()))); 137 148 138 149 //begin feature:0001827 … … 147 158 String originaleBase64 = base64.encode(Tools.getBytesFromFile(fichierAEnvoyer)); 148 159 Document reponseOriginale = (sessionManager.executeReturnDocument("pwg.images.addChunk", "data", 149 originaleBase64, "type", "file", "position", String.valueOf(i), "original_sum", Tools150 160 originaleBase64, "type", "file", "position", String.valueOf(i), "original_sum", 161 Tools.getMD5Checksum(image.getOriginale().getAbsolutePath()))); 151 162 if (!Tools.checkOk(reponseOriginale)) { 152 163 echec = true; … … 157 168 158 169 //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())); 164 176 LOG.debug("Response add : " + Tools.documentToString(reponseAjout)); 165 177 // System.out.println(Main.sessionManager.executerReturnString("pwg.images.add", "file_sum", Outil … … 189 201 */ 190 202 public boolean addTags(Integer imageId, String tagId) throws IOException, ProxyAuthenticationException { 191 Document doc = sessionManager.executeReturnDocument(MethodsEnum.SET_INFO.getLabel(), "image_id", String192 .valueOf(imageId), "tag_ids", tagId);203 Document doc = sessionManager.executeReturnDocument(MethodsEnum.SET_INFO.getLabel(), "image_id", 204 String.valueOf(imageId), "tag_ids", tagId); 193 205 try { 194 206 return Tools.checkOk(doc); … … 260 272 } 261 273 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 262 339 public SessionManager getSessionManager() { 263 340 return sessionManager; … … 267 344 this.sessionManager = sessionManager; 268 345 } 269 270 346 }
Note: See TracChangeset
for help on using the changeset viewer.