| @GET | |
| @Path("/") | |
| @Produces(MediaType.APPLICATION_JSON) | |
| @ApiOperation(value = "This method returns a list of activities", | |
| notes = "Default the latest ten activities will be returned") | |
| @ApiResponses(value = { | |
| @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "Returns a list of activities"), | |
| @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = "Not found"), | |
| @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal server problems") | |
| }) | |
| //TODO add filter | |
| public HttpResponse getActivities( | |
| @ApiParam(value = "Page number", required = false) @DefaultValue("0") @QueryParam("page") int page, | |
| @ApiParam(value = "Elements of components by page", required = false) @DefaultValue("10") @QueryParam("per_page") int perPage, | |
| @ApiParam(value = "User access token", required = false) @DefaultValue("") @QueryParam("access_token") String accessToken) { | |
| List<Activity> activities = new ArrayList<Activity>(); | |
| List<ActivityEx> activitiesEx = new ArrayList<ActivityEx>(); | |
| DALFacade dalFacade = null; | |
| PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); | |
| cm.setMaxTotal(20); | |
| CloseableHttpClient httpclient = HttpClients.custom() | |
| .setConnectionManager(cm) | |
| .build(); | |
| try { | |
| dalFacade = createConnection(); | |
| Gson gson = new Gson(); | |
| Pageable pageInfo = new PageInfo(page, perPage); | |
| activities = dalFacade.findActivities(pageInfo); | |
| ExecutorService executor = Executors.newCachedThreadPool(); | |
| HttpGet httpget; | |
| Future<String> future; | |
| JsonParser parser = new JsonParser(); | |
| for (Activity activity : activities) { | |
| ActivityEx activityEx = ActivityEx.getBuilderEx().activity(activity).build(); | |
| //TODO check if this runs paralel or if I need to create Threads | |
| try { | |
| if (!activity.getDataUrl().isEmpty()) { | |
| URIBuilder uriBuilder = new URIBuilder(activity.getDataUrl()); | |
| if (!accessToken.isEmpty()) { | |
| uriBuilder.setParameter("access_token", accessToken); | |
| } | |
| URI uri = uriBuilder.build(); | |
| httpget = new HttpGet(uri); | |
| future = executor.submit(new HttpRequestCallable(httpclient, httpget)); | |
| activityEx.setData(parser.parse(future.get())); | |
| } | |
| if (!activity.getUserUrl().isEmpty()) { | |
| URIBuilder uriBuilder = new URIBuilder(activity.getUserUrl()); | |
| if (!accessToken.isEmpty()) { | |
| uriBuilder.setParameter("access_token", accessToken); | |
| } | |
| URI uri = uriBuilder.build(); | |
| httpget = new HttpGet(uri); | |
| future = executor.submit(new HttpRequestCallable(httpclient, httpget)); | |
| activityEx.setUser(parser.parse(future.get())); | |
| } | |
| activitiesEx.add(activityEx); | |
| } catch (Exception ex) { | |
| if (ex instanceof ActivityTrackerException && | |
| ((ActivityTrackerException) ex).getErrorCode() == ErrorCode.AUTHORIZATION) { | |
| } else { | |
| throw ex; | |
| } | |
| } | |
| } | |
| executor.shutdown(); | |
| return new HttpResponse(gson.toJson(activitiesEx), HttpURLConnection.HTTP_OK); | |
| } catch (Exception ex) { | |
| ActivityTrackerException atException = ExceptionHandler.getInstance().convert(ex, ExceptionLocation.ACTIVITIESERVICE, ErrorCode.UNKNOWN, ""); | |
| return new HttpResponse(ExceptionHandler.getInstance().toJSON(atException), HttpURLConnection.HTTP_INTERNAL_ERROR); | |
| } finally { | |
| closeConnection(dalFacade); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment