Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

34 arc f5 save slack channels #59

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
2 changes: 2 additions & 0 deletions slack-archive/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ dependencies {
}
compile 'org.springframework.boot:spring-boot-starter-jetty'
compile 'org.springframework.boot:spring-boot-starter-data-mongodb'
compile 'org.apache.httpcomponents:httpclient:4.5.3'
compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
compile 'org.projectlombok:lombok:1.16.10'
compile 'javax.inject:javax.inject:1'
testCompile 'net.javacrumbs.json-unit:json-unit-fluent:1.19.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package juja.microservices.slack.archive;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

@Configuration
@EnableScheduling
public class SchedulerConfig implements SchedulingConfigurer {

private final int POOL_SIZE = 2;

@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(POOL_SIZE);
threadPoolTaskScheduler.setThreadNamePrefix("sheduled-get-slack-channels-task-pool-");
threadPoolTaskScheduler.initialize();

scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
package juja.microservices.slack.archive;

import org.apache.http.impl.client.HttpClients;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class SlackArchiveApplication {
public static void main(String[] args) {
SpringApplication.run(SlackArchiveApplication.class, args);
}

@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate(httpRequestFactory());
restTemplate.setMessageConverters(getHttpMessageConverters());
return restTemplate;
}

private ClientHttpRequestFactory httpRequestFactory() {
return new HttpComponentsClientHttpRequestFactory(HttpClients.createDefault());
}

private List<HttpMessageConverter<?>> getHttpMessageConverters() {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
converters.add(new StringHttpMessageConverter());
return converters;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package juja.microservices.slack.archive.api;

import juja.microservices.slack.archive.model.entity.RawChannel;

import java.util.List;

public interface SlackApiClient {

List<RawChannel> receiveRawChannelsList();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package juja.microservices.slack.archive.api;

import juja.microservices.slack.archive.exceptions.ArchiveException;
import juja.microservices.slack.archive.model.dto.ChannelsRawSlackResponse;
import juja.microservices.slack.archive.model.entity.RawChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@Slf4j
public class SlackApiClientImpl implements SlackApiClient {

private final RestTemplate restTemplate;

@Value("${slack.api.token}")
private String slackApiToken;
@Value("${slack.api.channels.urltemplate}")
private String slackApiChannelsUrlTemplate;

public SlackApiClientImpl(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

@Override
public List<RawChannel> receiveRawChannelsList() {

UriComponentsBuilder builder = UriComponentsBuilder
.fromUriString(slackApiChannelsUrlTemplate)
.queryParam("token", slackApiToken);

List<RawChannel> result = new LinkedList<>();
log.debug("Started request to slack api. Get channels list.");

try {
String cursor = "";
do {
ResponseEntity<ChannelsRawSlackResponse> response =
this.restTemplate.getForEntity(builder.toUriString(), ChannelsRawSlackResponse.class);
ChannelsRawSlackResponse slackResponse = response.getBody();

List<RawChannel> receivedChannels = slackResponse.getChannels()
.stream()
.map(channel -> new RawChannel(channel))
.collect(Collectors.toList());

result.addAll(receivedChannels);

if (slackResponse.getResponseMetadata() != null && slackResponse.getResponseMetadata().get("cursor") != null) {
cursor = (String) slackResponse.getResponseMetadata().get("cursor");
}
builder.queryParam("cursor", cursor);

} while (!cursor.equals(""));
} catch (HttpClientErrorException ex) {
throw new ArchiveException(ex.getMessage());
}

log.debug("Received channels : {}", result);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package juja.microservices.slack.archive.controller;

import juja.microservices.slack.archive.model.ChannelDTO;
import juja.microservices.slack.archive.model.MessagesRequest;
import juja.microservices.slack.archive.service.ArchiveService;
import juja.microservices.slack.archive.model.dto.ChannelDTO;
import juja.microservices.slack.archive.model.dto.MessagesRequest;
import juja.microservices.slack.archive.service.ChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -27,7 +27,7 @@
public class ArchiveController {

@Inject
private ArchiveService service;
private ChannelService service;

@PostMapping(value = "/messages/", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void archiveSlackMessage(@RequestParam("token") String token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
*/

public class ArchiveException extends RuntimeException {

public ArchiveException(String message, Throwable cause) {
super(message, cause);
}

public ArchiveException(String message) {
super(message);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package juja.microservices.slack.archive.model.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.*;


@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"name"
})
@Data
@AllArgsConstructor
public class ChannelDTO {
@JsonProperty("id")
private String channelId;
@JsonProperty("name")
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

package juja.microservices.slack.archive.model.dto;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Setter
@ToString
public class ChannelSlackResponse {

@JsonProperty("ok")
private Boolean ok;
@JsonProperty("channels")
private List<ChannelDTO> channels;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package juja.microservices.slack.archive.model.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ChannelsRawSlackResponse {

@JsonProperty("ok")
private String ok;

private List<Map<String, Object>> channels;

private Map<String, Object> responseMetadata;

public void addChannels(List<Map<String, Object>> channels) {

if (this.channels == null) {
channels = new LinkedList<>();
}
this.channels.addAll(channels);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package juja.microservices.slack.archive.model;
package juja.microservices.slack.archive.model.dto;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -7,6 +7,7 @@

@Getter
public class MessagesRequest {

@NonNull
private String channel;
@NonNull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package juja.microservices.slack.archive.model;
package juja.microservices.slack.archive.model.entity;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;

@Getter
@Data
@NoArgsConstructor
public class Channel {

@Id
private String id;
private String channelId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package juja.microservices.slack.archive.model;
package juja.microservices.slack.archive.model.entity;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need @entity annotation

Copy link
Author

@NikolayNN NikolayNN Dec 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i read spring docs. MongoTemplate doesn't require @entity annotation

9.5. Saving, Updating, and Removing Documents
https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo-template

and there are two ways with Id annotation.

  1. A property or field annotated with @id (org.springframework.data.annotation.Id) will be mapped to the _id field.
  2. A property or field without an annotation but named id will be mapped to the _id field.

I think it is a good case to use @id annotation explicitly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Nikolay


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -9,6 +9,7 @@

@Getter
public class Message {

@Id
private String id;
private String channel;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package juja.microservices.slack.archive.model.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;

import java.util.Map;

@Data
@NoArgsConstructor
public class RawChannel {

@Id
private String id;
private Map<String, Object> channel;

public RawChannel(Map<String, Object> channel) {
this.channel = channel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package juja.microservices.slack.archive.repository;

import juja.microservices.slack.archive.model.entity.Channel;
import juja.microservices.slack.archive.model.entity.RawChannel;

import java.util.List;

public interface ChannelRepository {
List<Channel> getChannels();

void saveRawChannels(List<RawChannel> channels);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package juja.microservices.slack.archive.repository;

import juja.microservices.slack.archive.model.Channel;
import juja.microservices.slack.archive.model.Message;
import juja.microservices.slack.archive.model.entity.Message;

import java.util.List;

public interface ArchiveRepository {
void saveMessage(Message message);
public interface MessageRepository {

List<Channel> getChannels();
void saveMessage(Message message);

List<Message> getMessages(String channel, int number);
}
Loading