KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > FlickrImpl


1 package example;
2
3 import java.util.*;
4 import java.util.logging.*;
5 import javax.jws.*;
6 import javax.xml.bind.*;
7 import example.data.*;
8
9 @WebService(endpointInterface="example.FlickrAPI")
10 public class FlickrImpl implements FlickrAPI {
11   private static final Logger log =
12     Logger.getLogger(FlickrImpl.class.getName());
13
14   private HashMap<String JavaDoc,FlickrUser> _emailToUserMap
15     = new HashMap<String JavaDoc,FlickrUser>();
16
17   private HashMap<String JavaDoc,FlickrUser> _usernameToUserMap
18     = new HashMap<String JavaDoc,FlickrUser>();
19
20   private HashMap<String JavaDoc,FlickrPerson> _infoMap
21     = new HashMap<String JavaDoc,FlickrPerson>();
22
23   private HashMap<String JavaDoc,FlickrGroups> _groupsMap
24     = new HashMap<String JavaDoc,FlickrGroups>();
25
26   private HashMap<String JavaDoc,ArrayList<FlickrPhotos.Photo>> _photoMap
27     = new HashMap<String JavaDoc,ArrayList<FlickrPhotos.Photo>>();
28
29   public FlickrImpl()
30   {
31     FlickrUser user1 = new FlickrUser();
32     user1.nsid = "12345678901@N01";
33     user1.username = "resin-caucho";
34
35     _emailToUserMap.put("resin@caucho.com", user1);
36     _usernameToUserMap.put("resin-caucho", user1);
37
38     FlickrPerson person1 = new FlickrPerson();
39     person1.nsid = "12345678901@N01";
40     person1.isadmin = 0;
41     person1.ispro = 0;
42     person1.iconserver = 2;
43     person1.realname = "Resin Caucho";
44     person1.mbox_sha1sum = "a11fc34be47a7ad1da8f670a26fa2b29f293c9fd";
45     person1.location = "La Jolla, California";
46     person1.photosurl = "http://www.flickr.com/photos/resin-caucho/";
47     person1.profileurl = "http://www.flickr.com/people/resin-caucho/";
48     person1.photos = new FlickrPerson.Photos();
49     person1.photos.firstdate = 1053200573;
50     person1.photos.firstdatetaken = "2003-10-08 17:32:04";
51     person1.photos.count = 342;
52
53     _infoMap.put("12345678901@N01", person1);
54
55     FlickrGroups groups1 = new FlickrGroups();
56
57     FlickrGroups.Group group1 = new FlickrGroups.Group();
58     group1.nsid = "23456789012@N01";
59     group1.name = "Flowers";
60     group1.admin = 0;
61     group1.eighteenplus = 0;
62     groups1.groups.add(group1);
63
64     FlickrGroups.Group group2 = new FlickrGroups.Group();
65     group2.nsid = "34567890123@N01";
66     group2.name = "Architecture";
67     group2.admin = 0;
68     group2.eighteenplus = 0;
69     groups1.groups.add(group2);
70
71     _groupsMap.put("12345678901@N01", groups1);
72
73     ArrayList<FlickrPhotos.Photo> photoList1 =
74       new ArrayList<FlickrPhotos.Photo>();
75
76     FlickrPhotos.Photo photo1 = new FlickrPhotos.Photo();
77     photo1.id = "3041";
78     photo1.owner = "12345678901@N01";
79     photo1.secret = "x123456";
80     photo1.server = 2;
81     photo1.title = "Our wedding";
82     photo1.ispublic = 1;
83     photo1.isfriend = 0;
84     photo1.isfamily = 0;
85     photoList1.add(photo1);
86
87     FlickrPhotos.Photo photo2 = new FlickrPhotos.Photo();
88     photo2.id = "3042";
89     photo2.owner = "12345678901@N01";
90     photo2.secret = "y123456";
91     photo2.server = 1;
92     photo2.title = "Best friends";
93     photo2.ispublic = 0;
94     photo2.isfriend = 1;
95     photo2.isfamily = 0;
96     photoList1.add(photo2);
97
98     _photoMap.put("12345678901@N01", photoList1);
99   }
100
101   @WebMethod(operationName="flickr.people.findByEmail")
102   public FlickrResponse
103   findByEmail(@WebParam(name="api_key") String JavaDoc api_key,
104               @WebParam(name="find_email") String JavaDoc find_email)
105   {
106     FlickrResponse response = new FlickrResponse();
107     response.payload = _emailToUserMap.get(find_email);
108
109     if (response.payload == null) {
110       response.stat = "fail";
111       response.payload = new FlickrError();
112     }
113    
114     return response;
115   }
116
117   @WebMethod(operationName="flickr.people.findByUsername")
118   public FlickrResponse
119   findByUsername(@WebParam(name="api_key") String JavaDoc api_key,
120                  @WebParam(name="username") String JavaDoc username)
121   {
122     FlickrResponse response = new FlickrResponse();
123     response.payload = _usernameToUserMap.get(username);
124    
125     if (response.payload == null) {
126       response.stat = "fail";
127       response.payload = new FlickrError();
128     }
129    
130     return response;
131   }
132
133   @WebMethod(operationName="flickr.people.getInfo")
134   public FlickrResponse
135   getInfo(@WebParam(name="api_key") String JavaDoc api_key,
136           @WebParam(name="user_id") String JavaDoc user_id)
137   {
138     FlickrResponse response = new FlickrResponse();
139     response.payload = _infoMap.get(user_id);
140
141     if (response.payload == null) {
142       response.stat = "fail";
143       response.payload = new FlickrError();
144     }
145    
146     return response;
147   }
148
149   @WebMethod(operationName="flickr.people.getPublicGroups")
150   public FlickrResponse
151   getPublicGroups(@WebParam(name="api_key") String JavaDoc api_key,
152                   @WebParam(name="user_id") String JavaDoc user_id)
153   {
154     FlickrResponse response = new FlickrResponse();
155     response.payload = _groupsMap.get(user_id);
156
157     if (response.payload == null) {
158       response.stat = "fail";
159       response.payload = new FlickrError();
160     }
161    
162     return response;
163   }
164
165   @WebMethod(operationName="flickr.people.getPublicPhotos")
166   public FlickrResponse
167   getPublicPhotos(@WebParam(name="api_key") String JavaDoc api_key,
168                   @WebParam(name="user_id") String JavaDoc user_id,
169                   @WebParam(name="extras") String JavaDoc extras,
170                   @WebParam(name="per_page") int per_page,
171                   @WebParam(name="page") int page)
172   {
173     FlickrResponse response = new FlickrResponse();
174
175     ArrayList<FlickrPhotos.Photo> photoList = _photoMap.get(user_id);
176
177     if (photoList != null) {
178       FlickrPhotos photos = new FlickrPhotos();
179       photos.page = page;
180       photos.pages = (photoList.size() / per_page) +
181                      (photoList.size() % per_page > 0 ? 1 : 0);
182       photos.perpage = per_page;
183       photos.total = photoList.size();
184
185       int startPhoto = (page - 1) * per_page;
186       int endPhoto = Math.min(page * per_page, photos.total);
187
188       for (int i = startPhoto; i < endPhoto; i++)
189         photos.photos.add(photoList.get(i));
190
191       response.payload = photos;
192     }
193
194     if (response.payload == null) {
195       response.stat = "fail";
196       response.payload = new FlickrError();
197     }
198    
199     return response;
200   }
201 }
202
Popular Tags