]> juplo.de Git - website/blob
8ba4d913f92dc02e4802b5f6b487a263b271bfaa
[website] /
1 ---
2 _edit_last: "2"
3 _wp_old_slug: develop-a-facebook-app-with-spring-social-part-07-whats-on-the-wire
4 author: kai
5 categories:
6   - howto
7 date: "2016-01-29T09:18:33+00:00"
8 guid: http://juplo.de/?p=694
9 parent_post_id: null
10 post_id: "694"
11 tags:
12   - createmedia.nrw
13   - facebook
14   - graph-api
15   - java
16   - oauth2
17   - spring
18   - spring-boot
19   - spring-social
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/
22
23 ---
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")
25
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.
27
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.
29
30 ## The Source is With You
31
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.
35
36 ## Why You Want To Listen On The Wire
37
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.
41
42 Fortunately, you can turn on the debugging of the underling classes, that process theses requests, to sidestep this problem.
43
44 ## Introducing HttpClient
45
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.
51
52 Hence, we will switch our configuration to use the `HttpClient` from Apache, before turning on the debug-logging.
53
54 ## Switching From Apaches `HttpCompnents` To `HttpClient`
55
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`:
57
58 ```Java
59 @Bean
60 public HttpComponentsClientHttpRequestFactory requestFactory(Environment env)
61 {
62   HttpComponentsClientHttpRequestFactory factory =
63       new HttpComponentsClientHttpRequestFactory();
64   factory.setConnectTimeout(
65       Integer.parseInt(env.getProperty("httpclient.timeout.connection"))
66       );
67   factory.setReadTimeout(
68       Integer.parseInt(env.getProperty("httpclient.timeout.read"))
69       );
70   return factory;
71 }
72
73 ```
74
75 To use this configuration, you also have to add the dependency `org.apache.httpcomonents:httpclient` in your `pom.xml`.
76
77 As you can see, this would also be the right place to enable other specialized configuration-options.
78
79 ## Logging The Headers From HTTP-Requests And Responses
80
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`
83
84 ## Take Full Control
85
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.
89
90 For example, to enable logging of the headers and the content of all requests, you have to start your app like this:
91
92 ```bash
93 mvn spring-boot:run \
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
99
100 ```
101
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.
103
104 ## Funded by the Europian Union
105
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.
109
110 [![Europäische Union: Investitionen in unsere Zukunft - Europäischer Fonds für regionale Entwicklung](/img/EFRE_Foerderhinweis_deutsch_farbig.svg)![EFRE.NRW 2014-2020: Invesitionen in Wachstum und Beschäftigung](/img/Ziel2NRW_4c_1809_eps.svg)](http://yourshouter.com/projekte/crowdgest%C3%BCtzte-veranstaltungs-suchmaschine.html "Show details about the funded resarch-project")