This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
/
ProfileTemplate.java
224 lines (175 loc) · 7.17 KB
/
ProfileTemplate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Copyright 2014 the original author or authors.
*
* 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
*
* https://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 org.springframework.social.linkedin.api.impl;
import static org.springframework.social.linkedin.api.impl.LinkedInTemplate.*;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.social.ApiException;
import org.springframework.social.linkedin.api.LinkedInProfile;
import org.springframework.social.linkedin.api.LinkedInProfileFull;
import org.springframework.social.linkedin.api.LinkedInProfiles;
import org.springframework.social.linkedin.api.ProfileField;
import org.springframework.social.linkedin.api.ProfileOperations;
import org.springframework.social.linkedin.api.SearchParameters;
import org.springframework.social.support.URIBuilder;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestOperations;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Class that implements operations for Profile API
*
* @author Robert Drysdale
* @author Rosty Kerei
*/
class ProfileTemplate extends AbstractTemplate implements ProfileOperations {
static {
StringBuffer b = new StringBuffer();
b.append(":(");
boolean first = true;
for (ProfileField f : ProfileField.values()) {
switch (f) {
case CONNECTIONS:
break;
default:
if (first) {
first = false;
}
else {
b.append(',');
}
b.append(f);
}
}
b.append(")?format=json");
FULL_PROFILE_FIELDS = b.toString();
}
private RestOperations restOperations;
private ObjectMapper objectMapper;
public ProfileTemplate(RestOperations restOperations, ObjectMapper objectMapper) {
this.restOperations = restOperations;
this.objectMapper = objectMapper;
}
public String getProfileId() {
return getUserProfile().getId();
}
public String getProfileUrl() {
return getUserProfile().getPublicProfileUrl();
}
public LinkedInProfile getUserProfile() {
return getUserProfile(PROFILE_FIELDS, LinkedInProfile.class);
}
public LinkedInProfileFull getUserProfileFull() {
return getUserProfile(FULL_PROFILE_FIELDS, LinkedInProfileFull.class);
}
public LinkedInProfile getProfileById(String id) {
return getProfileFullById(id, PROFILE_FIELDS, LinkedInProfile.class);
}
public LinkedInProfile getProfileByPublicUrl(String url) {
return getProfileByPublicUrl(url, PROFILE_FIELDS, LinkedInProfile.class);
}
public LinkedInProfileFull getProfileFullById(String id) {
return getProfileFullById(id, FULL_PROFILE_FIELDS, LinkedInProfileFull.class);
}
public LinkedInProfileFull getProfileFullByPublicUrl(String url) {
return getProfileByPublicUrl(url, FULL_PROFILE_FIELDS, LinkedInProfileFull.class);
}
public LinkedInProfiles search(SearchParameters parameters) {
JsonNode node = restOperations.getForObject(expand(PEOPLE_SEARCH_URL, parameters), JsonNode.class);
try {
return objectMapper.reader(LinkedInProfiles.class).readValue(node.path("people"));
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private <T> T getUserProfile(String fields, Class<T> type) {
return restOperations.getForObject(URIBuilder.fromUri(BASE_URL + "~" + fields).build(), type);
}
private <T> T getProfileFullById(String id, String fields, Class<T> type) {
return restOperations.getForObject(URIBuilder.fromUri(BASE_URL + "id=" + id + fields).build(), type);
}
private <T> T getProfileByPublicUrl(String url, String fields, Class<T> type) {
try {
URI uri = URIBuilder.fromUri(BASE_URL + "url=" + URLEncoder.encode(url, "UTF-8") + fields).build();
return restOperations.exchange(uri, HttpMethod.GET, new HttpEntity<String>(""), type).getBody();
} catch (UnsupportedEncodingException unlikely) {
unlikely.printStackTrace();
throw new ApiException("linkedin", "Unlikely unsupported encoding error", unlikely);
}
}
private URI expand(String url, SearchParameters parameters) {
URIBuilder uriBuilder =URIBuilder.fromUri(url);
if (parameters.getKeywords() != null) {
uriBuilder.queryParam("keywords", parameters.getKeywords());
}
if (parameters.getFirstName() != null) {
uriBuilder.queryParam("first-name", parameters.getFirstName());
}
if (parameters.getLastName() != null) {
uriBuilder.queryParam("last-name", parameters.getLastName());
}
if (parameters.getCompanyName() != null) {
uriBuilder.queryParam("company-name", parameters.getCompanyName());
}
if (parameters.getCurrentCompany() != null) {
uriBuilder.queryParam("current-company", parameters.getCurrentCompany().toString());
}
if (parameters.getTitle() != null) {
uriBuilder.queryParam("title", parameters.getTitle());
}
if (parameters.getCurrentTitle() != null) {
uriBuilder.queryParam("current-title", parameters.getCurrentTitle().toString());
}
if (parameters.getSchoolName() != null) {
uriBuilder.queryParam("school-name", parameters.getSchoolName());
}
if (parameters.getCurrentSchool() != null) {
uriBuilder.queryParam("current-school", parameters.getCurrentSchool().toString());
}
if (parameters.getCountryCode() != null) {
uriBuilder.queryParam("country-code", parameters.getCountryCode());
}
if (parameters.getPostalCode() != null) {
uriBuilder.queryParam("postal-code", parameters.getPostalCode());
}
if (parameters.getDistance() != null) {
uriBuilder.queryParam("distance", parameters.getDistance().toString());
}
uriBuilder.queryParam("start", String.valueOf(parameters.getStart()));
uriBuilder.queryParam("count", String.valueOf(parameters.getCount()));
if (parameters.getSort() != null) {
uriBuilder.queryParam("sort", parameters.getSort().toString());
}
// Processing facets
MultiValueMap<SearchParameters.FacetType, String> facets = parameters.getFacets();
for (SearchParameters.FacetType ft : facets.keySet()) {
StringBuilder facetString = new StringBuilder(ft.toString());
for (String value : facets.get(ft)) {
facetString.append(",");
facetString.append(value);
}
uriBuilder.queryParam("facet", facetString.toString());
}
return uriBuilder.build();
}
static final String PROFILE_FIELDS = ":(id,first-name,last-name,emailAddress,headline,industry,site-standard-profile-request,public-profile-url,picture-url,summary)?format=json";
static final String FULL_PROFILE_FIELDS;
static final String PEOPLE_SEARCH_URL = "https://api.linkedin.com/v1/people-search:(people:(id,first-name,last-name,headline,industry,site-standard-profile-request,public-profile-url,picture-url,summary,api-standard-profile-request))";
}