/**
 * Copyright 2012-2013 StackMob
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.stackmob.customcode;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.types.User;
import com.stackmob.core.customcode.CustomCodeMethod;
import com.stackmob.core.rest.ProcessedAPIRequest;
import com.stackmob.core.rest.ResponseToProcess;
import com.stackmob.sdkapi.SDKServiceProvider;

import java.net.HttpURLConnection;
import java.util.*;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class HelloWorld implements CustomCodeMethod {

  @Override
  public String getMethodName() {
    return "hello_world";
  }

  @Override
  public List<String> getParams() {
    return Arrays.asList("accessToken");
  }

  @Override
  public ResponseToProcess execute (ProcessedAPIRequest request, SDKServiceProvider serviceProvider) {
      Map<String, Object> map = new HashMap<String, Object>();

      String accessToken = "";
      JSONParser parser = new JSONParser();
      try {
          Object obj = parser.parse(request.getBody());
          JSONObject jsonObject = (JSONObject) obj;
          accessToken = (String) jsonObject.get("accessToken");
      } catch (ParseException pe) {

      }

      FacebookClient facebookClient = new DefaultFacebookClient("CAAGF19ZAX3kUBAFpoaA9f2zY3ZARZAHxp15jZBZCmVeqC0Y8mvvbg48UZAZB8oZCPKezTrO2WANHkXBEqRaeowxvNov2CVBurZBJ0cfhKJ6e8KurZCFQLDZBkJxg0TqBZCZAGtz2ZB0KX4J6vd7VkQapOYxIkZCttJOjFAamPK2ZBRYJle0UEuKYiu5Wiy0LUZAIqSX69kjCWRektMgFprAZDZD");

      User user = facebookClient.fetchObject("me", User.class);


      // Facebook facebook = f.getInstance();
      
      // ResponseList<Friend> myFriends = null;
      // try {
      //     myFriends = facebook.getFriends();
      // }
      // catch (Exception e) {

      // }
      
      map.put("msg", user.getName());


      return new ResponseToProcess(HttpURLConnection.HTTP_OK, map);
  }

}

@@ -985,16 +985,208 @@
 til.*;%0A%0A
+import com.stackmob.sdkapi.http.Header;%0Aimport com.stackmob.sdkapi.http.HttpService;%0Aimport com.stackmob.sdkapi.http.request.GetRequest;%0Aimport com.stackmob.sdkapi.http.response.HttpResponse;%0A
 import o
@@ -2021,541 +2021,731 @@
     
-FacebookClient facebookClient = new DefaultFacebookClient(%22CAAGF19ZAX3kUBAFpoaA9f2zY3ZARZAHxp15jZBZCmVeqC0Y8mvvbg48UZAZB8oZCPKezTrO2WANHkXBEqRaeowxvNov2CVBurZBJ0cfhKJ6e8KurZCFQLDZBkJxg0TqBZCZAGtz2ZB0KX4J6vd7VkQapOYxIkZCttJOjFAamPK2ZBRYJle0UEuKYiu5Wiy0LUZAIqSX69kjCWRektMgFprAZDZD%22);%0A%0A      User user = facebookClient.fetchObject(%22me%22, User.class
+String url = %22http://www.httpbin.org/get%22;%0A%0A      // Formulate request headers%0A      Header accept = new Header(%22Accept-Charset%22, %22utf-8%22);%0A      Header content = new Header(%22Content-Type%22, %22application/x-www-form-urlencoded%22);%0A%0A      Set%3CHeader%3E set = new HashSet();%0A      set.add(accept);%0A      set.add(content);%0A%0A      try %7B%0A          HttpService http = serviceProvider.getHttpService(
 );%0A%0A
-%0A
       /
-/ Facebook facebook = f.getInstance();%0A      %0A      // ResponseList%3CFriend%3E myFriends = null;%0A      // try %7B
+* In this Example we are going to be making a GET request%0A       * but PUT/POST/DELETE requests are also possible.%0A       */%0A          GetRequest req = new GetRequest(url,set);%0A          HttpResponse resp = http.get(req);%0A
 %0A      
-//
     
- myFriends = facebook.getFriends();%0A      // %7D
+int responseCode = resp.getCode();%0A          String responseBody = resp.getBody();%0A
 %0A      
-//
+%7D
  cat
@@ -2766,27 +2766,10 @@
 e) %7B
+%7D
 %0A
-%0A      // %7D%0A      
 %0A   
@@ -2790,22 +2790,15 @@
 g%22, 
-user.getName()
+%22hello%22
 );%0A%0A

use http service