Advertisement
Guest User

Untitled

a guest
Sep 9th, 2010
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.UnsupportedEncodingException;
  3. import java.net.URLEncoder;
  4. import org.json.JSONException;
  5. import org.json.JSONObject;
  6. import org.restlet.Client;
  7. import org.restlet.Request;
  8. import org.restlet.Response;
  9. import org.restlet.data.ChallengeResponse;
  10. import org.restlet.data.ChallengeScheme;
  11. import org.restlet.data.CharacterSet;
  12. import org.restlet.data.Method;
  13. import org.restlet.data.Protocol;
  14. import org.restlet.data.Status;
  15. import org.restlet.ext.json.JsonRepresentation;
  16. import org.restlet.representation.Representation;
  17. import org.restlet.resource.ResourceException;
  18.  
  19. import com.jsontest.Definitions;
  20.  
  21. public class DeviceHandler {
  22.  
  23.   public static Representation represent(Device device)
  24.       throws ResourceException {
  25.     JSONObject json = new JSONObject();
  26.  
  27.     try {
  28.       json.put("caption", device.getName());
  29.       json.put("type", device.getType().name());
  30.  
  31.     } catch (JSONException e) {
  32.       e.printStackTrace();
  33.       throw new ResourceException(Status.SERVER_ERROR_INTERNAL);
  34.     }
  35.     JsonRepresentation jr = new JsonRepresentation(json);
  36.     jr.setCharacterSet(CharacterSet.UTF_8);
  37.     return jr;
  38.   }
  39.  
  40.   public static void saveDevice(Device device) {
  41.     // - Device:
  42.     // |- String title: "Test Desktop"
  43.     // |- String id: "foobar123456"
  44.     // |- Enum type: "desktop"
  45.  
  46.     try {
  47.       Client client = new Client(Protocol.HTTP);
  48.  
  49.       Request request = new Request(Method.POST,
  50.           Definitions.gpodder_url_root
  51.               + "/api/2/devices/"
  52.               + URLEncoder.encode(Definitions.gpodder_username,
  53.                   Definitions.encoding)
  54.               + "/"
  55.               + URLEncoder.encode(device.getId(),
  56.                   Definitions.encoding) + ".json",
  57.           represent(device));     // (method, uri, content)
  58.  
  59.       ChallengeResponse authentication = new ChallengeResponse(
  60.           ChallengeScheme.HTTP_BASIC, Definitions.gpodder_username,
  61.           Definitions.gpodder_password);
  62.       request.setChallengeResponse(authentication);
  63.  
  64.       Response response = client.handle(request);
  65.       System.out.println("Entity: #########################\n"
  66.           + request.getEntityAsText() + "\n");
  67.       System.out.println("Response: #######################\n"
  68.           + response.getEntity().getText() + "\n");
  69.  
  70.     } catch (UnsupportedEncodingException e) {
  71.       e.printStackTrace();
  72.     } catch (ResourceException e) {
  73.       e.printStackTrace();
  74.     } catch (IOException e) {
  75.       e.printStackTrace();
  76.     }
  77.  
  78.     // ####### Resulting console output #######
  79.     //
  80.     // Entity: #########################
  81.     // {"caption":"Test Desktop","type":"desktop"}
  82.     //
  83.     // Response: #######################
  84.     // <?xml version="1.0" encoding="iso-8859-1"?>
  85.     // <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  86.     // "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  87.     // <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  88.     // <head>
  89.     // <title>411 - Length Required</title>
  90.     // </head>
  91.     // <body>
  92.     // <h1>411 - Length Required</h1>
  93.     // </body>
  94.     // </html>
  95.     //
  96.   }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement