3 _wp_old_slug: develop-a-facebook-app-with-spring-social-part-07-whats-on-the-wire
7 date: "2016-01-29T09:18:33+00:00"
8 guid: http://juplo.de/?p=694
20 title: 'Develop a Facebook-App with Spring-Social – Part VII: What is Going On On The Wire'
21 url: /develop-a-facebook-app-with-spring-social-part-07-what-is-going-on-on-the-wire/
24 In this series of Mini-How-Tow's I will describe how to develop a facebook app with the help of [Spring-Social](http://projects.spring.io/spring-social/ "Learn more about Spring-Social")
26 In [the last part of this series](/develop-a-facebook-app-with-spring-social-part-06-sign-in-users-through-the-canvas-page "Read part 6 of this series to learn, how you turn your spring-social-app into a real facebook-app"), I showed you, how you can sign-in your users through the `signed_request`, that is send to your canvas-page.
28 In this part, I will show you, how to turn on logging of the HTTP-requests, that your app sends to, and the -responses it recieves from the Facebook Graph-API.
30 ## The Source is With You
32 You can find the source-code on [/git/examples/facebook-app/](/git/examples/facebook-app/ "Link for cloning")
33 and [browse it via gitweb](/gitweb/?p=examples/facebook-app;a=summary "Browse the source-code now").
34 Check out `part-07` to get the source for this part of the series.
36 ## Why You Want To Listen On The Wire
38 If you are developing your app, you will often wonder, why something does not work as expected.
39 In this case, it is often very usefull to be able to debug the communitation between your app and the Graph-API.
40 But since all requests to the Graph-API are secured by SSL you can not simply listen in with tcpdump or wireshark.
42 Fortunately, you can turn on the debugging of the underling classes, that process theses requests, to sidestep this problem.
44 ## Introducing HttpClient
46 In its default-configuration, the Spring Framework will use the `HttpURLConnection`, which comes with the JDK, as http-client.
47 As described in the [documentation](http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#rest-client-access "Read more about that in the Spring-documentation"), some advanced methods are not available, when using `HttpURLConnection`
48 Besides, the package [`HttpClient`](https://hc.apache.org/httpcomponents-client-4.5.x/index.html "Visit the project home of Apache HttpClient"), which is part of Apaches `HttpComponents` is a much more mature, powerful and configurable alternative.
49 For example, you easily can plug in connection pooling, to speed up the connection handling, or caching to reduce the amount of requests that go through the wire.
50 In production, you should always use this implementation, instead of the default-one, that comes with the JDK.
52 Hence, we will switch our configuration to use the `HttpClient` from Apache, before turning on the debug-logging.
54 ## Switching From Apaches `HttpCompnents` To `HttpClient`
56 To siwtch from the default client, that comes with the JDK to Apaches `HttpClient`, you have to configure an instance of `HttpComponentsClientHttpRequestFactory` as `HttpRequestFactory` in your `SocialConfig`:
60 public HttpComponentsClientHttpRequestFactory requestFactory(Environment env)
62 HttpComponentsClientHttpRequestFactory factory =
63 new HttpComponentsClientHttpRequestFactory();
64 factory.setConnectTimeout(
65 Integer.parseInt(env.getProperty("httpclient.timeout.connection"))
67 factory.setReadTimeout(
68 Integer.parseInt(env.getProperty("httpclient.timeout.read"))
75 To use this configuration, you also have to add the dependency `org.apache.httpcomonents:httpclient` in your `pom.xml`.
77 As you can see, this would also be the right place to enable other specialized configuration-options.
79 ## Logging The Headers From HTTP-Requests And Responses
81 I configured a short-cut to enable the logging of the HTTP-headers of the communication between the app and the Graph-API.
82 Simply run the app with the additionally switch `-Dhttpclient.logging.level=DEBUG`
86 If the headers are not enough to answer your questions, you can enable a lot more debugging messages.
87 You just have to overwrite the default logging-levels.
88 Read [the original documentation of `HttpClient`](https://hc.apache.org/httpcomponents-client-4.5.x/logging.html "Jump to the logging-guide form HttpClient now."), for more details.
90 For example, to enable logging of the headers and the content of all requests, you have to start your app like this:
94 -Dfacebook.app.id=YOUR_ID \
95 -Dfacebook.app.secret=YOUR_SECRET \
96 -Dfacebook.app.canvas=https://apps.facebook.com/YOUR_NAMESPACE
97 -Dlogging.level.org.apache.http=DEBUG \
98 -Dlogging.level.org.apache.http.wire=DEBUG
102 The second switch is necessary, because I defined the default-level `ERROR` for that logger in our `src/main/application.properties`, to enable the short-cut for logging only the headers.
104 ## Funded by the Europian Union
106 This article was published in the course of a
107 [resarch-project](http://yourshouter.com/projekte/crowdgest%C3%BCtzte-veranstaltungs-suchmaschine.html "Show details about the funded resarch-project"),
108 that is funded by the European Union and the federal state Northrhine-Wetphalia.
110 [](http://yourshouter.com/projekte/crowdgest%C3%BCtzte-veranstaltungs-suchmaschine.html "Show details about the funded resarch-project")