OpenRatClient.java (15150B)
1 package de.openrat.client; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.UnsupportedEncodingException; 6 import java.net.SocketTimeoutException; 7 import java.util.ArrayList; 8 import java.util.HashMap; 9 import java.util.Iterator; 10 import java.util.LinkedHashMap; 11 import java.util.List; 12 import java.util.Map; 13 14 import org.json.JSONArray; 15 import org.json.JSONException; 16 import org.json.JSONObject; 17 18 import android.util.Log; 19 import de.openrat.android.client.FolderEntry; 20 import de.openrat.android.client.FolderEntry.FType; 21 import de.openrat.android.client.util.FileUtils; 22 23 /** 24 * Komfortabler Zugriff auf das OpenRat-CMS. 25 * 26 * @author Jan Dankert 27 * 28 */ 29 public class OpenRatClient extends CMSRequest 30 { 31 32 private String token; 33 34 /** 35 * 36 */ 37 public OpenRatClient() 38 { 39 super(); 40 } 41 42 /** 43 * @param host 44 * @param path 45 * @param port 46 */ 47 public OpenRatClient(String host, String path, int port) 48 { 49 super(host, path, port); 50 } 51 52 /** 53 * @param host 54 * @param path 55 */ 56 public OpenRatClient(String host, String path) 57 { 58 super(host, path); 59 } 60 61 /** 62 * @param host 63 */ 64 public OpenRatClient(String host) 65 { 66 super(host); 67 } 68 69 /** 70 * Ermittelt den Inhalt eines Ordners. 71 * 72 * @return Id des höchsten Ordners. 73 */ 74 public String getRootFolder() throws IOException 75 { 76 clearParameters(); 77 setAction("tree"); 78 setActionMethod("show"); 79 setMethod("GET"); 80 81 readJSON(); 82 83 84 85 clearParameters(); 86 setAction("tree"); 87 setActionMethod("loadbranch"); 88 setParameter("type","project"); 89 setMethod("GET"); 90 91 try 92 { 93 final String folderid = readJSON().getJSONObject("output").getJSONArray("branch").getJSONObject(0).getString("id"); 94 return folderid; 95 } 96 catch (JSONException e) 97 { 98 throw new OpenRatClientException("JSON-Error while resolving root folder", e); 99 } 100 101 } 102 103 /** 104 * Liest den Inhalt für ein Seitenelement. 105 * 106 * @return 107 */ 108 public Map<String, String> getValue(String pageid, String elementid) throws IOException 109 { 110 clearParameters(); 111 setAction("pageelement"); 112 setActionMethod("edit"); 113 setId(pageid); 114 setParameter("elementid", elementid); 115 116 JSONObject json = readJSON(); 117 118 try 119 { 120 final Map<String, String> properties = new HashMap<String, String>(); 121 122 properties.put("type", json.getString("type")); 123 properties.put("text", json.optString("text")); 124 return properties; 125 } 126 catch (JSONException e) 127 { 128 throw new OpenRatClientException("JSON-Error while resolving root folder", e); 129 } 130 } 131 132 /** 133 * Setzt einen neuen Inhalt für ein Seitenelement. 134 * 135 * @return 136 */ 137 public void setValue(String pageid, String elementid, String type, String value, boolean release, boolean publish) 138 throws IOException 139 { 140 clearParameters(); 141 setAction("pageelement"); 142 setActionMethod("save"); 143 setId(pageid); 144 setParameter("elementid", elementid); 145 setParameter("text", value); 146 setParameter("release", release ? "1" : "0"); 147 setParameter("publish", publish ? "1" : "0"); 148 setMethod("POST"); 149 150 readJSON(); 151 } 152 153 /** 154 * Ermittelt den Inhalt eines Ordners. 155 * 156 * @param folderid 157 * Id des zu ladenen Ordners. Falls <code>null</code>, wird der 158 * Rootfolder geladen. 159 * @return Ordner-Einträge 160 */ 161 public List<FolderEntry> getFolderEntries(String folderid) throws IOException 162 { 163 final List<FolderEntry> data = new ArrayList<FolderEntry>(); 164 165 super.clearParameters(); 166 super.setMethod("GET"); 167 super.setParameter("id", folderid); 168 169 super.setParameter("action", "folder"); 170 super.setParameter("subaction", "edit"); 171 172 try 173 { 174 JSONObject json = readJSON().getJSONObject("output"); 175 if (!(json.get("object") instanceof JSONObject)) 176 return data; // Ordner ist leer. 177 178 JSONObject inhalte = json.getJSONObject("object"); 179 JSONArray names = inhalte.names(); 180 181 for (int i = 0; i < names.length(); i++) 182 { 183 JSONObject obj = inhalte.getJSONObject(names.getString(i)); 184 185 final FolderEntry entry = new FolderEntry(); 186 entry.type = FType.valueOf(obj.getString("type").toUpperCase()); 187 entry.name = obj.getString("name"); 188 entry.description = obj.getString("desc"); 189 entry.id = names.getString(i); 190 data.add(entry); 191 192 } 193 194 } 195 catch (JSONException e) 196 { 197 throw new OpenRatClientException("Coult not load folder "+folderid, e); 198 } 199 200 return data; 201 } 202 203 protected JSONObject readJSON() throws OpenRatClientException 204 { 205 byte[] response; 206 try 207 { 208 response = super.performRequest(); 209 Log.d("client", "Server-Response:\n" + new String(response, "UTF-8")); 210 } 211 catch (SocketTimeoutException e) 212 { 213 throw new OpenRatClientException("Timeout exceeded", e); 214 } 215 catch (IOException e) 216 { 217 throw new OpenRatClientException("I/O-Error while performing the request", e); 218 } 219 220 try 221 { 222 JSONObject json; 223 try 224 { 225 json = new JSONObject(new String(response, "UTF-8")); 226 } 227 catch (UnsupportedEncodingException e1) 228 { 229 throw new OpenRatClientException("UTF-8 not supported?!", e1); 230 } 231 232 try 233 { 234 // Versuchen, die 1. Notice zu lesen. Falls es eine gibt, 235 // Exception mit dem Notice-Text werfen. 236 final String status = json.getString("status"); 237 238 if (!status.equalsIgnoreCase("ok")) 239 { 240 String msgText; 241 try 242 { 243 msgText = json.getJSONArray("notices").getJSONObject(0).getString("text"); 244 } 245 catch (JSONException e) 246 { 247 msgText = "Not OK (Server response does not include \"OK\" and does not include a notice text"; 248 } 249 throw new OpenRatClientException(msgText); 250 } 251 else 252 return json; // Alles OK. 253 } 254 catch (JSONException e) 255 { 256 Log.w("client","Server error: Response does not include a attribute 'notice_status'."); 257 // throw new OpenRatClientException( 258 // "Server error: Response does not include a attribute 'notice_status'.\n"+response); 259 return json; // Alles OK, kann passieren, wenn es keine Notices 260 // gibt. 261 } 262 263 } 264 catch (JSONException e) 265 { 266 throw new OpenRatClientException("JSON Parsing Error. Original response was:\n" + new String(response) 267 + "\n\n", e); 268 } 269 } 270 271 /** 272 * @author dankert 273 * 274 */ 275 public class OpenRatClientException extends IOException 276 { 277 /** 278 * 279 */ 280 public OpenRatClientException() 281 { 282 super(); 283 } 284 285 /** 286 * @param throwable 287 */ 288 public OpenRatClientException(Throwable throwable) 289 { 290 super(throwable.getMessage()); 291 } 292 293 private Throwable cause; 294 295 /** 296 * @param detailMessage 297 */ 298 public OpenRatClientException(String detailMessage) 299 { 300 super(detailMessage); 301 } 302 303 /** 304 * @param detailMessage 305 */ 306 public OpenRatClientException(String detailMessage, Throwable cause) 307 { 308 super(detailMessage); 309 this.cause = cause; 310 } 311 312 /** 313 * {@inheritDoc} 314 * 315 * @see java.lang.Throwable#getCause() 316 */ 317 @Override 318 public Throwable getCause() 319 { 320 return this.cause; 321 } 322 323 } 324 325 /** 326 * @throws IOException 327 * 328 */ 329 public void uploadFile(String filenName, File file) throws IOException 330 { 331 332 super.clearParameters(); 333 super.setAction("folder"); 334 super.setActionMethod("createnewfile"); 335 super.setMethod("POST"); 336 337 byte[] fileBytes; 338 try 339 { 340 fileBytes = FileUtils.getBytesFromFile(file); 341 } 342 catch (IOException e) 343 { 344 throw new OpenRatClientException(e); 345 } 346 super.setFile(filenName, fileBytes, file.getName(), "image/jpeg", "binary"); 347 348 @SuppressWarnings("unused") 349 final JSONObject response = readJSON(); 350 } 351 352 /** 353 * @param type 354 * Typ 355 * @param id 356 * Id 357 * @throws IOException 358 */ 359 public void publish(String type, String id) throws IOException 360 { 361 362 super.clearParameters(); 363 super.setAction(type); 364 super.setActionMethod("pub"); 365 super.setId(id); 366 367 // Erstmal alles aktivieren was geht 368 // TODO: Abfrage der gewünschten Einstellungen über AlertDialog. 369 super.setParameter("subdirs", "1"); 370 super.setParameter("pages", "1"); 371 super.setParameter("files", "1"); 372 373 @SuppressWarnings("unused") 374 JSONObject response = readJSON(); 375 } 376 377 public void login(String login, String password, String database) throws IOException 378 { 379 super.setParameter("action", "login"); 380 super.setParameter("subaction", "login"); 381 382 JSONObject json = readJSON(); 383 try 384 { 385 setMethod("GET"); 386 JSONObject session = json.getJSONObject("session"); 387 388 final String sessionName = session.getString("name"); 389 final String sessionId = session.getString("id"); 390 this.token = session.getString("token"); 391 setCookie(sessionName, sessionId); 392 393 if (database.length() > 0) 394 super.setParameter("dbid", database); 395 super.setParameter("login_name", login); 396 super.setParameter("login_password", password); 397 super.setParameter("token", this.token); 398 super.setMethod("POST"); 399 400 json = readJSON(); 401 } 402 catch (JSONException e) 403 { 404 try 405 { 406 final String msgText = json.getJSONArray("notices").getJSONObject(0).getString("text"); 407 throw new OpenRatClientException(msgText, e); 408 } 409 catch (JSONException e1) 410 { 411 throw new OpenRatClientException(e); 412 } 413 } 414 } 415 416 public List<FolderEntry> loadProjects() throws IOException 417 { 418 419 List<FolderEntry> data = new ArrayList<FolderEntry>(); 420 421 super.setMethod("GET"); 422 super.clearParameters(); 423 super.setParameter("action", "start"); 424 super.setParameter("subaction", "projectmenu"); 425 426 try 427 { 428 JSONObject json = readJSON().getJSONObject("output"); 429 JSONArray projects = json.getJSONArray("projects"); 430 431 for (int i = 0; i < projects.length(); i++) 432 { 433 JSONObject project = projects.getJSONObject(i); 434 435 final FolderEntry entry = new FolderEntry(); 436 entry.type = FType.PROJECT; 437 entry.name = project.getString("name"); 438 entry.description = ""; 439 entry.id = project.getString("id"); 440 441 data.add(entry); 442 443 } 444 } 445 catch (JSONException e) 446 { 447 Log.e(this.getClass().getName(), e.getMessage(), e); 448 } 449 450 return data; 451 } 452 453 /** 454 * Wählt ein Projekt. 455 * 456 * @param projectid 457 * Projekt-ID. 458 * @throws IOException 459 */ 460 public void selectProject(String projectid) throws IOException 461 { 462 463 super.clearParameters(); 464 super.setAction("start"); 465 super.setActionMethod("projectmenu"); 466 super.setParameter("id", projectid); 467 super.setParameter("token", this.token); 468 super.setMethod("POST"); 469 470 readJSON(); 471 } 472 473 public Map<String, String> getPageElements(String id) throws IOException 474 { 475 476 Map<String, String> el = new HashMap<String, String>(); 477 super.clearParameters(); 478 super.setAction("page"); 479 super.setActionMethod("el"); 480 super.setId(id); 481 482 JSONObject json = readJSON(); 483 484 final Map<String, String> elementMap = new LinkedHashMap<String, String>(); 485 486 try 487 { 488 JSONObject elements = json.getJSONObject("el"); 489 490 for (Iterator ti = elements.keys(); ti.hasNext();) 491 { 492 String elementId = (String) ti.next(); 493 String pageelementName = elements.getJSONObject(elementId).getString("name"); 494 elementMap.put(elementId, pageelementName); 495 } 496 } 497 catch (JSONException e) 498 { 499 Log.w(this.getClass().getSimpleName(), "\n\n" + json); 500 throw new OpenRatClientException("a property was not found", e); 501 } 502 503 return elementMap; 504 505 } 506 507 public Map<String, String> getProperties(String type, String id) throws IOException 508 { 509 super.clearParameters(); 510 super.setAction(type); 511 super.setActionMethod("prop"); 512 super.setId(id); 513 514 JSONObject json = readJSON(); 515 516 final Map<String, String> properties = new HashMap<String, String>(); 517 518 try 519 { 520 properties.put("name", json.getString("name")); 521 properties.put("filename", json.getString("filename")); 522 properties.put("description", json.getString("description")); 523 } 524 catch (JSONException e) 525 { 526 throw new OpenRatClientException("a property was not found", e); 527 } 528 529 return properties; 530 531 } 532 533 public void setProperties(String type, String id, Map<String, String> properties) throws IOException 534 { 535 super.clearParameters(); 536 super.setAction(type); 537 if (type.equals("page")) 538 super.setActionMethod("prop"); 539 else 540 super.setActionMethod("saveprop"); 541 super.setMethod("POST"); 542 super.setId(id); 543 544 for (String name : properties.keySet()) 545 { 546 super.setParameter(name, properties.get(name)); 547 } 548 549 readJSON(); 550 } 551 552 public Map<String, String> getTemplates() throws IOException 553 { 554 super.clearParameters(); 555 super.setAction("template"); 556 super.setActionMethod("listing"); 557 super.setMethod("POST"); 558 559 JSONObject json = readJSON(); 560 561 final Map<String, String> templateMap = new LinkedHashMap<String, String>(); 562 563 try 564 { 565 JSONObject templates = json.getJSONObject("templates"); 566 567 for (Iterator ti = templates.keys(); ti.hasNext();) 568 { 569 String templateId = (String) ti.next(); 570 String templateName = templates.getJSONObject(templateId).getString("name"); 571 templateMap.put(templateId, templateName); 572 } 573 } 574 catch (JSONException e) 575 { 576 Log.w(this.getClass().getSimpleName(), "\n\n" + json); 577 throw new OpenRatClientException("a property was not found", e); 578 } 579 580 return templateMap; 581 } 582 583 public void createFolder(String folderid, String string) throws IOException 584 { 585 super.clearParameters(); 586 super.setId(folderid); 587 super.setAction("folder"); 588 super.setActionMethod("createnewfolder"); 589 super.setMethod("POST"); 590 super.setParameter("name", string); 591 592 readJSON(); 593 } 594 595 public void createPage(String folderid, String string, String templateid) throws IOException 596 { 597 super.clearParameters(); 598 super.setId(folderid); 599 super.setAction("folder"); 600 super.setActionMethod("createnewpage"); 601 super.setMethod("POST"); 602 super.setParameter("name", string); 603 super.setParameter("templateid", templateid); 604 605 readJSON(); 606 } 607 608 public void delete(String folderid, String ids) throws IOException 609 { 610 super.clearParameters(); 611 super.setMethod("POST"); 612 super.setAction("folder"); 613 super.setActionMethod("multiple"); 614 super.setId(folderid); 615 616 super.setParameter("type", "delete"); 617 super.setParameter("ids", ids); 618 super.setParameter("commit", "1"); 619 620 readJSON(); 621 622 } 623 624 public byte[] getFileContent(String objectid) throws IOException 625 { 626 super.clearParameters(); 627 super.setMethod("GET"); 628 super.setAction("file"); 629 super.setActionMethod("show"); 630 super.setId(objectid); 631 632 final byte[] content = performRequest(); 633 return content; 634 } 635 636 public Map<String, String> getLanguages() throws IOException 637 { 638 super.clearParameters(); 639 super.setMethod("GET"); 640 super.setAction("language"); 641 super.setActionMethod("listing"); 642 643 final Map<String, String> languageMap = new LinkedHashMap<String, String>(); 644 645 final JSONObject json = readJSON(); 646 try 647 { 648 JSONObject languageObject = json.getJSONObject("el"); 649 650 for (Iterator ti = languageObject.keys(); ti.hasNext();) 651 { 652 String id = (String) ti.next(); 653 String name = languageObject.getJSONObject(id).getString("name"); 654 languageMap.put(id, name); 655 } 656 } 657 catch (JSONException e) 658 { 659 Log.w(this.getClass().getSimpleName(), "\n\n" + json); 660 throw new OpenRatClientException("a property was not found", e); 661 } 662 return languageMap; 663 664 } 665 666 public void setLanguage(String languageid) throws IOException 667 { 668 super.clearParameters(); 669 super.setMethod("GET"); 670 super.setAction("index"); 671 super.setActionMethod("language"); 672 super.setId(languageid); 673 674 readJSON(); 675 } 676 }