Skip to content

Commit

Permalink
Add Tweet when Mizo return home. fixes #308 @6H
Browse files Browse the repository at this point in the history
  • Loading branch information
みぞ@CrazyBeatCoder committed Feb 2, 2018
1 parent 5820ed5 commit ad9be70
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 160 deletions.
30 changes: 13 additions & 17 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mizo-location.iml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/lily-white-line-notify-1.0-SNAPSHOT/WEB-INF/classes" />
<output url="file://$MODULE_DIR$/target/mizo-location-1.0-SNAPSHOT/WEB-INF/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Copyright 2015 Google Inc.
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.mizo0203.lilywhite</groupId>
<artifactId>lily-white-line-notify</artifactId>
<groupId>com.mizo0203.mizo-location</groupId>
<artifactId>mizo-location</artifactId>
<dependencies>
<dependency>
<groupId>com.google.appengine</groupId>
Expand Down
33 changes: 0 additions & 33 deletions src/main/java/com/mizo0203/lilywhite/domain/Define.java

This file was deleted.

6 changes: 0 additions & 6 deletions src/main/java/com/mizo0203/lilywhite/domain/Translator.java

This file was deleted.

66 changes: 0 additions & 66 deletions src/main/java/com/mizo0203/lilywhite/domain/UseCase.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.appspot.mizo310ki;
package com.mizo0203.location;

import com.mizo0203.lilywhite.domain.UseCase;
import com.mizo0203.location.domain.UseCase;

import javax.mail.Address;
import javax.mail.MessagingException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,26 @@
* limitations under the License.
*/

package com.mizo0203.lilywhite.push_task;
package com.mizo0203.location;

import com.mizo0203.lilywhite.domain.UseCase;
import com.mizo0203.location.domain.UseCase;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.logging.Logger;

public class ReminderTaskServlet extends HttpServlet {
public class PushTaskServlet extends HttpServlet {
public static final String PARAM_NAME_SOURCE_ID = "param_name_source_id";
public static final String PARAM_NAME_MESSAGE = "param_name_message";
private static final Logger LOG = Logger.getLogger(ReminderTaskServlet.class.getName());
private UseCase mUseCase;
private static final Logger LOG = Logger.getLogger(PushTaskServlet.class.getName());

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
mUseCase = new UseCase();
try {
try (UseCase useCase = new UseCase()) {
String source_id = req.getParameter(PARAM_NAME_SOURCE_ID);
String message = req.getParameter(PARAM_NAME_MESSAGE);
// mUseCase.pushReminderMessage(source_id, message);
// mUseCase.initSource(source_id);
} finally {
mUseCase.close();
useCase.pushReminderMessage(source_id, message);
}
}
}
59 changes: 59 additions & 0 deletions src/main/java/com/mizo0203/location/domain/UseCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.mizo0203.location.domain;

import com.mizo0203.location.repo.Repository;
import twitter4j.Twitter;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.Closeable;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public class UseCase implements Closeable {
private static final Logger LOG = Logger.getLogger(UseCase.class.getName());
private final Repository mRepository;

public UseCase() {
mRepository = new Repository();
}

@Override
public void close() {
mRepository.destroy();
}

public void onReceiveMizoLocationMail(String name, MimeMessage message)
throws MessagingException {
switch (name) {
case "enter":
onEnter(message);
break;
case "exit":
onExit(message);
break;
default:
break;
}
}

private void onEnter(MimeMessage message) throws MessagingException {
String subject = message.getSubject();
long etaMillis = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(4);
mRepository.enqueueReminderTask("", etaMillis, subject);
}

private void onExit(MimeMessage message) {
mRepository.deleteReminderTask();
}

public void pushReminderMessage(String source_id, String message) {
Twitter mizo0203 = mRepository.createTwitterInstance();
try {
mizo0203.updateStatus("帰宅ったー");
} catch (Exception e) {
LOG.warning(e.getMessage());
} finally {
mRepository.deleteReminderTask();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mizo0203.lilywhite.repo;
package com.mizo0203.location.repo;

import com.googlecode.objectify.ObjectifyService;
import com.mizo0203.lilywhite.repo.objectify.entity.KeyEntity;
import com.mizo0203.location.repo.objectify.entity.KeyEntity;

/* package */ class OfyRepository {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.mizo0203.lilywhite.repo;
package com.mizo0203.location.repo;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.mizo0203.lilywhite.push_task.ReminderTaskServlet;
import com.mizo0203.location.PushTaskServlet;

import javax.annotation.Nonnull;
import java.util.logging.Logger;
Expand Down Expand Up @@ -38,8 +38,8 @@ protected String enqueueReminderTask(String source_id, long etaMillis, String me
return mQueue
.add(
TaskOptions.Builder.withUrl("/push_task/reminder_task")
.param(ReminderTaskServlet.PARAM_NAME_SOURCE_ID, source_id)
.param(ReminderTaskServlet.PARAM_NAME_MESSAGE, message)
.param(PushTaskServlet.PARAM_NAME_SOURCE_ID, source_id)
.param(PushTaskServlet.PARAM_NAME_MESSAGE, message)
.etaMillis(etaMillis))
.getName();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mizo0203.lilywhite.repo;
package com.mizo0203.location.repo;

import com.mizo0203.lilywhite.repo.objectify.entity.KeyEntity;
import com.mizo0203.location.repo.objectify.entity.KeyEntity;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
Expand All @@ -24,7 +24,7 @@ public void destroy() {
mPushQueueRepository.destroy();
}

private void deleteReminderTask() {
public void deleteReminderTask() {
String taskName = getKey("taskName");
if (taskName == null || taskName.isEmpty()) {
return;
Expand Down Expand Up @@ -69,7 +69,7 @@ private String getKey(String key) {
mOfyRepository.saveKeyEntity(keyEntity);
}
if (keyEntity.value.isEmpty()) {
LOG.severe(key + " isEmpty");
LOG.severe(key + " isEmpty"); // TODO: 正常系ログ
}
return keyEntity.value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mizo0203.lilywhite.repo;
package com.mizo0203.location.repo;

public enum State {
NO_REMINDER_MESSAGE, //
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mizo0203.lilywhite.repo.objectify;
package com.mizo0203.location.repo.objectify;

import com.googlecode.objectify.ObjectifyService;
import com.mizo0203.lilywhite.repo.objectify.entity.KeyEntity;
import com.mizo0203.location.repo.objectify.entity.KeyEntity;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.mizo0203.lilywhite.repo.objectify.entity;
package com.mizo0203.location.repo.objectify.entity;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.mizo0203.lilywhite.repo.objectify.OfyHelper;
import com.mizo0203.location.repo.objectify.OfyHelper;

/**
* The @Entity tells Objectify about our entity. We also register it in {@link OfyHelper} Our
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mizo0203.lilywhite.util;
package com.mizo0203.location.util;

import org.apache.commons.io.IOUtils;

Expand Down
12 changes: 6 additions & 6 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<servlet>
<servlet-name>mailhandler</servlet-name>
<servlet-class>com.appspot.mizo310ki.MailHandlerServlet</servlet-class>
<servlet-class>com.mizo0203.location.MailHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mailhandler</servlet-name>
Expand All @@ -29,12 +29,12 @@
<!-- [START Push Task] -->
<!-- https://cloud.google.com/appengine/docs/standard/java/taskqueue/push/ -->
<servlet>
<servlet-name>reminder_task</servlet-name>
<servlet-class>com.mizo0203.lilywhite.push_task.ReminderTaskServlet</servlet-class>
<servlet-name>push_task</servlet-name>
<servlet-class>com.mizo0203.location.PushTaskServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>reminder_task</servlet-name>
<url-pattern>/push_task/reminder_task</url-pattern>
<servlet-name>push_task</servlet-name>
<url-pattern>/push_task/*</url-pattern>
</servlet-mapping>

<!-- https://cloud.google.com/appengine/docs/standard/java/config/cron#securing_urls_for_cron -->
Expand All @@ -58,6 +58,6 @@
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.mizo0203.lilywhite.repo.objectify.OfyHelper</listener-class>
<listener-class>com.mizo0203.location.repo.objectify.OfyHelper</listener-class>
</listener>
</web-app>

0 comments on commit ad9be70

Please sign in to comment.