-
Notifications
You must be signed in to change notification settings - Fork 10
/
LinkedInScribe.java
187 lines (165 loc) · 5.9 KB
/
LinkedInScribe.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
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.seam.social.linkedin;
import java.io.StringWriter;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.New;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.jboss.seam.social.linkedin.model.Profile;
import org.jboss.seam.social.linkedin.model.Update;
import org.jboss.seam.social.linkedin.model.UpdateJaxb;
import org.jboss.seam.social.oauth.HttpResponse;
import org.jboss.seam.social.oauth.OAuthService;
import org.jboss.seam.social.oauth.OAuthServiceScribe;
import org.jboss.seam.social.oauth.OAuthServiceSettings;
import org.jboss.seam.social.oauth.RelatedTo;
import org.jboss.seam.social.oauth.RestVerb;
import org.jboss.seam.social.oauth.Service;
import org.jboss.seam.social.oauth.UserProfile;
import org.scribe.builder.api.Api;
import org.scribe.builder.api.LinkedInApi;
/**
* @author Antoine Sabot-Durand
*/
@RelatedTo(Service.LinkedIn)
public class LinkedInScribe extends OAuthServiceScribe implements LinkedIn {
private static final long serialVersionUID = -6718362913575146613L;
static final String USER_PROFILE_URL = "http://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,picture-url,site-standard-profile-request:(url))";
static final Class<? extends Api> API_CLASS = LinkedInApi.class;
static final String LOGO_URL = "https://d2l6uygi1pgnys.cloudfront.net/1-9-05/images/buttons/linkedin_connect.png";
static final String TYPE = "LinkedIn";
static final String NETWORK_UPDATE_URL = "http://api.linkedin.com/v1/people/~/person-activities";
JAXBContext context;
Unmarshaller unmarshaller;
Marshaller marshaller;
@PostConstruct
protected void init() {
try {
context = JAXBContext.newInstance("org.jboss.seam.social.linkedin.model");
unmarshaller = context.createUnmarshaller();
marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
@Inject
public void setSettings(@RelatedTo(Service.LinkedIn) OAuthServiceSettings settings) {
super.setSettings(settings);
}
@Produces
@RelatedTo(Service.LinkedIn)
protected OAuthService qualifiedProducer(@New LinkedInScribe linkedIn)
{
return linkedIn;
}
/*
* (non-Javadoc)
*
* @see org.jboss.seam.social.oauth.OAuthServiceScribe#getApiClass()
*/
@Override
protected Class<? extends Api> getApiClass() {
return API_CLASS;
}
/*
* (non-Javadoc)
*
* @see org.jboss.seam.social.oauth.OAuthService#getServiceLogo()
*/
@Override
public String getServiceLogo() {
return LOGO_URL;
}
/*
* (non-Javadoc)
*
* @see org.jboss.seam.social.oauth.OAuthService#getUserProfile()
*/
@Override
public UserProfile getUser() {
if (session.getUserProfile() == null) {
HttpResponse resp = sendSignedRequest(RestVerb.GET, USER_PROFILE_URL);
try {
// System.out.println(StreamUtils.getStreamContents(resp.getStream()));
session.setUserProfile((UserProfile) unmarshaller.unmarshal(resp.getStream()));
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return session.getUserProfile();
}
protected Profile getLinkedInProfile() {
return (Profile) getUser();
}
/*
* (non-Javadoc)
*
* @see org.jboss.seam.social.oauth.OAuthService#getType()
*/
@Override
public String getType() {
return TYPE;
}
/*
* (non-Javadoc)
*
* @see org.jboss.seam.social.oauth.HasStatus#updateStatus()
*/
@Override
public Object updateStatus() {
return updateStatus(getStatus());
}
/*
* (non-Javadoc)
*
* @see org.jboss.seam.social.oauth.HasStatus#updateStatus(java.lang.String)
*/
@Override
public Update updateStatus(String message) {
Update upd = new UpdateJaxb();
String msg = "<a href=\"" + getLinkedInProfile().getStandardProfileUrl() + "\">" + getUser().getFullName() + "</a> "
+ message;
upd.setBody(msg);
StringWriter writer = new StringWriter();
try {
marshaller.marshal(upd, writer);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String update = writer.toString();
HttpResponse resp = sendSignedXmlRequest(RestVerb.POST, NETWORK_UPDATE_URL, update);
if (resp.getCode() == 201) {
setStatus("");
// everything is ok should notify caller
} else {
// something went wrong should we throw an exception ?
}
return upd;
}
}