Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.restlet.Client;
- import org.restlet.Request;
- import org.restlet.Response;
- import org.restlet.data.ChallengeResponse;
- import org.restlet.data.ChallengeScheme;
- import org.restlet.data.CharacterSet;
- import org.restlet.data.Method;
- import org.restlet.data.Protocol;
- import org.restlet.data.Status;
- import org.restlet.ext.json.JsonRepresentation;
- import org.restlet.representation.Representation;
- import org.restlet.resource.ResourceException;
- import com.jsontest.Definitions;
- public class DeviceHandler {
- public static Representation represent(Device device)
- throws ResourceException {
- JSONObject json = new JSONObject();
- try {
- json.put("caption", device.getName());
- json.put("type", device.getType().name());
- } catch (JSONException e) {
- e.printStackTrace();
- throw new ResourceException(Status.SERVER_ERROR_INTERNAL);
- }
- JsonRepresentation jr = new JsonRepresentation(json);
- jr.setCharacterSet(CharacterSet.UTF_8);
- return jr;
- }
- public static void saveDevice(Device device) {
- // - Device:
- // |- String title: "Test Desktop"
- // |- String id: "foobar123456"
- // |- Enum type: "desktop"
- try {
- Client client = new Client(Protocol.HTTP);
- Request request = new Request(Method.POST,
- Definitions.gpodder_url_root
- + "/api/2/devices/"
- + URLEncoder.encode(Definitions.gpodder_username,
- Definitions.encoding)
- + "/"
- + URLEncoder.encode(device.getId(),
- Definitions.encoding) + ".json",
- represent(device)); // (method, uri, content)
- ChallengeResponse authentication = new ChallengeResponse(
- ChallengeScheme.HTTP_BASIC, Definitions.gpodder_username,
- Definitions.gpodder_password);
- request.setChallengeResponse(authentication);
- Response response = client.handle(request);
- System.out.println("Entity: #########################\n"
- + request.getEntityAsText() + "\n");
- System.out.println("Response: #######################\n"
- + response.getEntity().getText() + "\n");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (ResourceException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- // ####### Resulting console output #######
- //
- // Entity: #########################
- // {"caption":"Test Desktop","type":"desktop"}
- //
- // Response: #######################
- // <?xml version="1.0" encoding="iso-8859-1"?>
- // <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- // "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- // <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- // <head>
- // <title>411 - Length Required</title>
- // </head>
- // <body>
- // <h1>411 - Length Required</h1>
- // </body>
- // </html>
- //
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement