1616import com .topcoder .direct .services .project .metadata .entities .dao .DirectProjectMetadata ;
1717import com .topcoder .direct .services .view .action .AbstractAction ;
1818import com .topcoder .direct .services .view .action .BaseDirectStrutsAction ;
19- import com .topcoder .direct .services .view .dto .contest .TermOfUse ;
2019import com .topcoder .direct .services .view .action .specreview .ViewSpecificationReviewActionResultData ;
2120import com .topcoder .direct .services .view .dto .IdNamePair ;
22- import com .topcoder .direct .services .view .dto .contest .BaseContestCommonDTO ;
23- import com .topcoder .direct .services .view .dto .contest .ContestBriefDTO ;
24- import com .topcoder .direct .services .view .dto .contest .ContestDashboardDTO ;
25- import com .topcoder .direct .services .view .dto .contest .ContestRoundType ;
26- import com .topcoder .direct .services .view .dto .contest .ContestStatsDTO ;
27- import com .topcoder .direct .services .view .dto .contest .ContestStatus ;
28- import com .topcoder .direct .services .view .dto .contest .PhasedContestDTO ;
29- import com .topcoder .direct .services .view .dto .contest .ProjectPhaseDTO ;
30- import com .topcoder .direct .services .view .dto .contest .ProjectPhaseType ;
21+ import com .topcoder .direct .services .view .dto .contest .*;
3122import com .topcoder .direct .services .view .dto .cost .CostDTO ;
3223import com .topcoder .direct .services .view .dto .project .ProjectBriefDTO ;
3324import com .topcoder .direct .services .view .interceptor .SecurityGroupsAccessInterceptor ;
7364import com .topcoder .shared .util .DBMS ;
7465import com .topcoder .shared .util .dwload .CacheClearer ;
7566import com .topcoder .web .common .CachedDataAccess ;
67+ import com .topcoder .web .common .cache .CacheClient ;
68+ import com .topcoder .web .common .cache .CacheClientFactory ;
7669import com .topcoder .web .common .cache .MaxAge ;
7770import eu .medsea .mimeutil .MimeType ;
7871import eu .medsea .mimeutil .MimeUtil ;
7972import org .apache .axis .encoding .Base64 ;
8073import org .apache .commons .io .FilenameUtils ;
8174import org .apache .commons .io .IOUtils ;
75+ import org .apache .http .HttpEntity ;
76+ import org .apache .http .HttpHeaders ;
77+ import org .apache .http .HttpResponse ;
78+ import org .apache .http .HttpStatus ;
79+ import org .apache .http .client .methods .HttpGet ;
80+ import org .apache .http .client .utils .URIBuilder ;
81+ import org .apache .http .impl .client .DefaultHttpClient ;
8282import org .apache .log4j .Logger ;
8383import org .apache .struts2 .ServletActionContext ;
8484import org .apache .struts2 .util .TokenHelper ;
85+ import org .codehaus .jackson .JsonNode ;
86+ import org .codehaus .jackson .map .ObjectMapper ;
8587import org .springframework .web .context .WebApplicationContext ;
8688import org .springframework .web .context .support .WebApplicationContextUtils ;
8789
9597import javax .xml .datatype .DatatypeConfigurationException ;
9698import javax .xml .datatype .DatatypeFactory ;
9799import javax .xml .datatype .XMLGregorianCalendar ;
98- import java .io .BufferedInputStream ;
99- import java .io .ByteArrayInputStream ;
100- import java .io .ByteArrayOutputStream ;
101- import java .io .File ;
102- import java .io .FileInputStream ;
103- import java .io .FileOutputStream ;
104- import java .io .IOException ;
105- import java .io .InputStream ;
100+ import java .io .*;
106101import java .nio .channels .FileLock ;
107102import java .sql .Connection ;
108103import java .sql .PreparedStatement ;
109104import java .sql .ResultSet ;
110105import java .text .DateFormat ;
111106import java .text .ParseException ;
112107import java .text .SimpleDateFormat ;
113- import java .util .ArrayList ;
114- import java .util .Arrays ;
115- import java .util .Calendar ;
116- import java .util .Collection ;
117- import java .util .Collections ;
118- import java .util .Comparator ;
119- import java .util .Date ;
120- import java .util .GregorianCalendar ;
121- import java .util .HashMap ;
122- import java .util .HashSet ;
123- import java .util .Iterator ;
124- import java .util .LinkedHashMap ;
125- import java .util .LinkedList ;
126- import java .util .List ;
127- import java .util .Map ;
128- import java .util .Set ;
129- import java .util .TimeZone ;
108+ import java .util .*;
130109import java .util .zip .ZipEntry ;
131110import java .util .zip .ZipInputStream ;
132111import java .util .zip .ZipOutputStream ;
@@ -952,6 +931,14 @@ public final class DirectUtils {
952931 private static final String QUERY_GET_SECURITY_GROUP_FROM_ID = "SELECT group_id, description FROM security_groups " +
953932 " WHERE group_id in (" ;
954933
934+ /**
935+ * The jackson object mapping which is used to deserialize json return from API to domain model.
936+ */
937+ protected static final ObjectMapper objectMapper ;
938+ static {
939+ objectMapper = new ObjectMapper ();
940+ }
941+
955942 /**
956943 * <p>
957944 * Default Constructor.
@@ -3832,4 +3819,76 @@ public static List<ProjectGroup> getGroupIdAndName(List<ProjectGroup> projectGro
38323819 DatabaseUtils .close (con );
38333820 }
38343821 }
3822+
3823+ /**
3824+ * Get group from group API.
3825+ *
3826+ * @param tcSubject tcSubject of user
3827+ * @param endpoint endpoint url
3828+ * @return set of group
3829+ * @throws Exception
3830+ */
3831+ public static Set <ProjectGroup > getGroupsFromApi (TCSubject tcSubject , String endpoint ) throws Exception {
3832+ URIBuilder uri = new URIBuilder (endpoint );
3833+
3834+ if (!DirectUtils .isCockpitAdmin (tcSubject ) && !DirectUtils .isTcStaff (tcSubject )) {
3835+ uri .setParameter ("memberId" , String .valueOf (tcSubject .getUserId ()));
3836+ }
3837+
3838+ DefaultHttpClient httpClient = new DefaultHttpClient ();
3839+
3840+ HttpGet getRequest = new HttpGet (uri .build ());
3841+ logger .info ("Getting Group with thi uri: " + uri .build ().toString ());
3842+
3843+ String v3Token = new JwtTokenUpdater ().check ().getToken ();
3844+
3845+ getRequest .setHeader (HttpHeaders .AUTHORIZATION , "Bearer " + v3Token );
3846+
3847+ getRequest .addHeader (HttpHeaders .ACCEPT , "application/json" );
3848+ HttpResponse httpResponse = httpClient .execute (getRequest );
3849+
3850+ HttpEntity entity = httpResponse .getEntity ();
3851+
3852+ if (httpResponse .getStatusLine ().getStatusCode () != HttpStatus .SC_OK ) {
3853+ throw new Exception ("Unable to get groups from the API:" + httpResponse .getStatusLine ().getReasonPhrase ());
3854+ }
3855+
3856+ JsonNode result = objectMapper .readTree (entity .getContent ());
3857+ JsonNode groups = result .path ("result" ).path ("content" );
3858+ Set <ProjectGroup > groupResults = new HashSet <ProjectGroup >();
3859+ for (JsonNode group : groups ) {
3860+ ProjectGroup pg = new ProjectGroup (group .get ("id" ).asLong (), group .get ("name" ).asText ());
3861+ groupResults .add (pg );
3862+ }
3863+ return groupResults ;
3864+ }
3865+
3866+ /**
3867+ * Get groups. Get from cache first if none then get from api
3868+ *
3869+ * @param tcSubject tcSubject of user
3870+ * @param endpoint endpoint url
3871+ * @return set of groupfor user
3872+ * @throws Exception
3873+ */
3874+ public static Set <ProjectGroup > getGroups (TCSubject tcSubject , String endpoint ) throws Exception {
3875+ CacheClient cc = null ;
3876+ Set <ProjectGroup > projectGroups = null ;
3877+ SortedCacheAddress cacheAddress = new SortedCacheAddress (tcSubject .getUserId ());
3878+ try {
3879+ cc = CacheClientFactory .create ();
3880+ projectGroups = (Set <ProjectGroup >) cc .get (cacheAddress );
3881+ } catch (Exception e ) {
3882+ logger .info ("Can't get group for user " + tcSubject .getUserId () + " from cache" );
3883+ }
3884+ if (projectGroups == null ) {
3885+ projectGroups = DirectUtils .getGroupsFromApi (tcSubject , endpoint );
3886+ try {
3887+ cc .set (cacheAddress , projectGroups , MaxAge .HOUR );
3888+ } catch (Exception e ) {
3889+ logger .error ("Failed to put user group into cache " , e );
3890+ }
3891+ }
3892+ return projectGroups ;
3893+ }
38353894}
0 commit comments