View Javadoc
1   package org.woehlke.logfileloader.eai.pipelines;
2   
3   import org.slf4j.Logger;
4   import org.slf4j.LoggerFactory;
5   import org.springframework.integration.annotation.MessageEndpoint;
6   import org.woehlke.logfileloader.core.entities.*;
7   import org.woehlke.logfileloader.core.services.*;
8   import org.woehlke.logfileloader.eai.events.ProcessOneLogfileLineEvent;
9   
10  import javax.inject.Inject;
11  import java.text.ParseException;
12  import java.text.SimpleDateFormat;
13  import java.util.ArrayList;
14  import java.util.Date;
15  import java.util.Locale;
16  
17  /**
18   * Created with IntelliJ IDEA.
19   * User: Fert
20   * Date: 02.09.13
21   * Time: 14:10
22   * To change this template use File | Settings | File Templates.
23   */
24  @MessageEndpoint("processLogfileLinesPipeline")
25  public class PostProcessingPipeline {
26  
27      private final static Logger LOGGER = LoggerFactory.getLogger(PostProcessingPipeline.class);
28  
29      @Inject
30      private LogfileLineItemService logfileLineItemService;
31  
32      @Inject
33      private BrowserService browserService;
34  
35      @Inject
36      private DayService dayService;
37  
38      @Inject
39      private HttpCodeService httpCodeService;
40  
41      @Inject
42      private IpService ipService;
43  
44      @Inject
45      private RequestService requestService;
46  
47      public ProcessOneLogfileLineEvent log(ProcessOneLogfileLineEvent event) {
48          //LOGGER.info(event.toString());
49          return event;
50      }
51  
52      public ProcessOneLogfileLineEvent getIpNumber(ProcessOneLogfileLineEvent event) {
53          String line = event.getLine().getLine();
54          String ipString = line.split(" ")[0];
55          try {
56              if(ipService.find(ipString)==null){
57                  ipService.createOrFetch(ipString);
58              }
59          } catch (Exception e){
60              LOGGER.error(e.getMessage());
61          }
62          event.setIp(ipString);
63          return event;
64      }
65  
66      public ProcessOneLogfileLineEvent getTimeStamp(ProcessOneLogfileLineEvent event) {
67          String line = event.getLine().getLine();
68          String rest = line.split("\\[")[1];
69          String datetimeString = rest.split("\\]")[0].split(" ")[0];
70          //LOGGER.info("datetimeString: "+datetimeString);
71          String timezoneString = rest.split("\\]")[0].split(" ")[1];
72          //LOGGER.info("timezoneString: "+timezoneString);
73          Locale locales[] = {Locale.GERMAN,Locale.ENGLISH};
74          for(final Locale locale:locales){
75              SimpleDateFormat parserSDF = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", locale);
76              Date date = null;
77              try {
78                  int timezone = Integer.parseInt(timezoneString);
79                  Date datetime = parserSDF.parse(datetimeString);
80                  timezone -= 100; //CEST
81                  timezone *= 60 * 1000;
82                  long timestamp = datetime.getTime() + timezone;
83                  date = new Date(timestamp);
84                  event.setDatetime(date);
85                  if(dayService.find(date)==null){
86                      dayService.createOrFetch(date);
87                  }
88              } catch (NumberFormatException u) {
89                  LOGGER.error(u.getMessage());
90              } catch (ParseException e) {
91                  LOGGER.error(e.getMessage());
92              } catch (Exception e) {
93                  LOGGER.warn(e.getMessage());
94              }
95              //LOGGER.info("### "+event.toString());
96          }
97          return event;
98      }
99  
100     public ProcessOneLogfileLineEvent getRequestString(ProcessOneLogfileLineEvent event) {
101         String line = event.getLine().getLine();
102         String requestLine = "UNDEFINED";
103         try {
104             requestLine = line.split("\"")[1].split(" ")[1];
105         } catch (ArrayIndexOutOfBoundsException e) {
106             LOGGER.warn(e.getMessage());
107         }
108         event.setRequestLine(requestLine);
109         try {
110             if(requestService.find(requestLine)==null){
111                 requestService.createOrFetch(requestLine);
112             }
113         } catch (Exception e) {
114             LOGGER.warn(e.getMessage());
115         }
116         //LOGGER.info(requestLine);
117         return event;
118     }
119 
120     public ProcessOneLogfileLineEvent getHttpCode(ProcessOneLogfileLineEvent event) {
121         String line = event.getLine().getLine();
122         String httpCode = "UNDEFINED";
123         try {
124             httpCode = line.split("\"")[2].split(" ")[1];
125         } catch (ArrayIndexOutOfBoundsException e) {
126             LOGGER.warn(e.getMessage());
127         }
128         event.setHttpCode(httpCode);
129         try {
130             if(httpCodeService.find(httpCode)==null){
131                 httpCodeService.createOrFetch(httpCode);
132             }
133         } catch (Exception e) {
134             LOGGER.warn(e.getMessage());
135         }
136         //LOGGER.info(httpCode);
137         return event;
138     }
139 
140     public ProcessOneLogfileLineEvent getBrowser(ProcessOneLogfileLineEvent event) {
141         String line = event.getLine().getLine();
142         String browser = "UNDEFINED";
143         try {
144             browser = line.split("\"")[5];
145         } catch (ArrayIndexOutOfBoundsException e) {
146             LOGGER.warn(e.getMessage());
147         }
148         event.setBrowser(browser);
149         try {
150             if(browserService.find(browser)==null){
151                 browserService.createOrFetch(browser);
152             }
153         } catch (Exception e) {
154             LOGGER.warn(e.getMessage());
155         }
156         //LOGGER.info(browser);
157         return event;
158     }
159 
160     public ProcessOneLogfileLineEvent pushIntoDatabase(ProcessOneLogfileLineEvent event) {
161         //LOGGER.info("pushIntoDatabase: "+event.toString());
162         LogfileLineItem logfileLineItem = new LogfileLineItem();
163         logfileLineItem.setLine(event.getLine().getLine());
164         logfileLineItem.setTime(event.getDatetime());
165         Browser browser = browserService.find(event.getBrowser());
166         Day day = dayService.find(event.getDatetime());
167         HttpCode httpCode = httpCodeService.find(event.getHttpCode());
168         Ip ip = ipService.find(event.getIp());
169         Request request = requestService.find(event.getRequestLine());
170         logfileLineItem.setBrowser(browser);
171         logfileLineItem.setIp(ip);
172         logfileLineItem.setHttpCode(httpCode);
173         logfileLineItem.setRequest(request);
174         logfileLineItem.setDay(day);
175         logfileLineItemService.save(logfileLineItem);
176         return event;
177     }
178 }