Skip to content

Commit

Permalink
Add LINE Notify API "POST https://notify-api.line.me/api/notify". fix…
Browse files Browse the repository at this point in the history
…es #304 @1h
  • Loading branch information
みぞ@CrazyBeatCoder committed Jan 29, 2018
1 parent 85b5a8b commit f5b4e2c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/mizo0203/lilywhite/RedirectServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
LOG.info("error_description:\t" + req.getParameter("error_description"));
try (UseCase useCase = new UseCase()) {
useCase.tokenOauth(req.getParameter("code"));
useCase.notify("Hello, LINE Notify World");
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/mizo0203/lilywhite/domain/UseCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mizo0203.lilywhite.repo.Repository;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;

Expand All @@ -25,6 +26,10 @@ public void tokenOauth(String code) {
mRepository.tokenOauth(code);
}

public void notify(@Nonnull String message) {
mRepository.notify(message);
}

public interface AuthorizeOauthCallback {
void onBuildRedirectUrlString(@Nullable String redirectUrlString) throws IOException;
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/mizo0203/lilywhite/repo/LineRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mizo0203.lilywhite.util.HttpUtil;
import org.apache.http.client.utils.URIBuilder;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
Expand All @@ -20,6 +21,8 @@
"https://notify-bot.line.me/oauth/authorize";
private static final String LINE_NOTIFY_API_TOKEN_OAUTH_URL_STR =
"https://notify-bot.line.me/oauth/token";
private static final String LINE_NOTIFY_API_NOTIFY_URL_STR =
"https://notify-api.line.me/api/notify";

@SuppressWarnings("EmptyMethod")
public void destroy() {
Expand Down Expand Up @@ -62,4 +65,32 @@ public void tokenOauth(
e.printStackTrace();
}
}

public void notify(
@Nonnull String access_token,
@Nonnull String message,
@Nullable String imageThumbnail,
@Nullable String imageFullsize,
@Nullable String imageFile,
@Nullable Number stickerPackageId,
@Nullable Number stickerId,
@Nullable HttpUtil.Callback callback) {
Map<String, String> reqProp = new HashMap<>();
reqProp.put("Content-Type", "application/x-www-form-urlencoded");
reqProp.put("Authorization", "Bearer " + access_token);

Map<String, String> params = new HashMap<>();
params.put("message", message);
params.put("imageThumbnail", imageThumbnail);
params.put("imageFullsize", imageFullsize);
params.put("imageFile", imageFile);
params.put("stickerPackageId", stickerPackageId != null ? stickerPackageId.toString() : null);
params.put("stickerId", stickerId != null ? stickerId.toString() : null);

try {
HttpUtil.post(new URL(LINE_NOTIFY_API_NOTIFY_URL_STR), reqProp, params, callback);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/mizo0203/lilywhite/repo/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mizo0203.lilywhite.domain.Define;
import com.mizo0203.lilywhite.repo.line.data.AccessToken;
import com.mizo0203.lilywhite.repo.line.data.ResponseNotifyData;
import com.mizo0203.lilywhite.repo.objectify.entity.KeyEntity;
import org.apache.commons.io.IOUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.net.HttpURLConnection;
Expand Down Expand Up @@ -55,6 +57,26 @@ private void onResponseTokenOauth(HttpURLConnection connection) {
}
}

public void notify(@Nonnull String message) {
String access_token = getKey("access_token");
mLineRepository.notify(
access_token, message, null, null, null, null, null, this::onResponseNotify);
}

private void onResponseNotify(HttpURLConnection connection) {
try {
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return;
}
String body = IOUtils.toString(connection.getInputStream(), StandardCharsets.UTF_8);
ResponseNotifyData responseNotifyData =
new ObjectMapper().readValue(body, ResponseNotifyData.class);
LOG.info("responseNotifyData: " + responseNotifyData.getMessage());
} catch (IOException e) {
LOG.log(Level.SEVERE, "", e);
}
}

private void setKey(String key, String value) {
KeyEntity keyEntity = mOfyRepository.loadKeyEntity(key);
if (keyEntity == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mizo0203.lilywhite.repo.line.data;

public class ResponseNotifyData {
private int status;
private String message;

public int getStatus() {
return status;
}

public String getMessage() {
return message;
}
}

0 comments on commit f5b4e2c

Please sign in to comment.