net.yatomiya.nicherry.services.bbs.PostMessageHandler.java Source code

Java tutorial

Introduction

Here is the source code for net.yatomiya.nicherry.services.bbs.PostMessageHandler.java

Source

/*******************************************************************************
 * Copyright (c) 2014,2015 Hideki Yatomi
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package net.yatomiya.nicherry.services.bbs;

import java.io.*;
import java.util.*;
import com.squareup.okhttp.*;
import net.yatomiya.e4.services.*;
import net.yatomiya.e4.util.*;
import net.yatomiya.nicherry.*;
import net.yatomiya.nicherry.model.bbs.*;
import net.yatomiya.nicherry.services.bbs.model.*;

public class PostMessageHandler {
    private ThreadProcessor processor;
    private boolean isSynchronous;
    private Call httpCall;
    private long executeTime;

    private String name;
    private String mail;
    private String body;

    private PostMessageEvent postEvent;

    public ThreadProcessor getProcessor() {
        return processor;
    }

    public MThread getModel() {
        return processor.getModel();
    }

    public long getExecuteTime() {
        return executeTime;
    }

    public Call execute(ThreadProcessor processor, boolean isSynchronous, String name, String mail, String body,
            Map<String, String> additionalParams) {
        this.processor = processor;
        this.isSynchronous = isSynchronous;
        this.name = name;
        this.mail = mail;
        this.body = body;

        executeTime = JUtils.getCurrentTime();

        EUtils.get(EventService.class).send(BBSService.Event.Post.START, getModel());

        BBSService bbsSrv = processor.getManager().getBBSService();
        BBSHttpClient client = bbsSrv.getHttpClient();

        ThreadId threadId = getModel().getId();
        MBoard board = bbsSrv.getBoard(threadId.getBoardId());
        String hostUrl = HttpUtils.getHostUrl(board.getUrl());
        String postUrl = hostUrl + "/test/bbs.cgi";

        Request.Builder builder = new Request.Builder();
        builder.url(postUrl);

        Map<String, String> valueMap = new HashMap<>();
        valueMap.put("bbs", threadId.getBoardId().getIdValue());
        valueMap.put("key", threadId.getIdValue());
        valueMap.put("time", String.valueOf((JUtils.getCurrentTime() / 1000) - 60));
        valueMap.put("submit", "??");
        valueMap.put("FROM", name);
        valueMap.put("mail", mail);
        valueMap.put("MESSAGE", body);
        if (additionalParams != null)
            valueMap.putAll(additionalParams);
        String valueStr = HttpUtils.buildFormPostData(valueMap, NConstants.CHARSET_SHIFT_JIS);

        builder.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded; charset=Shift_JIS"),
                StringUtils.getBytes(valueStr, NConstants.CHARSET_SHIFT_JIS)));

        builder.header("Referer", board.getUrl());

        Request request = builder.build();

        Callback callback = new Callback() {
            @Override
            public void onResponse(Response response) throws IOException {
                EUtils.asyncExec(() -> doOnResponse(response));
            }

            @Override
            public void onFailure(Request request, IOException e) {
                EUtils.asyncExec(() -> doOnFailure(request, e));
            }
        };
        httpCall = client.execute(request, callback, isSynchronous);

        postEvent = new PostMessageEvent(name, mail, body);

        return httpCall;
    }

    void doOnResponse(Response response) {
        int statusCode = response.code();

        try {
            String responseBody = StringUtils.getString(response.body().bytes(), NConstants.CHARSET_SHIFT_JIS);
            postEvent.setupWithResponse(getModel(), response.request(), response, responseBody);
        } catch (Exception e) {
            postEvent.setupConnectionError(getModel(), response.request(), e);
        }

        doFinish();
    }

    void doOnFailure(Request request, IOException e) {
        postEvent.setupConnectionError(getModel(), request, e);
        doFinish();
    }

    void doFinish() {
        getProcessor().postFinished(postEvent);

        EUtils.get(EventService.class).send(BBSService.Event.Post.END, postEvent);

        {
            String msg = "";
            switch (postEvent.getType()) {
            case SUCCESS:
                msg = "?????????";
                break;
            case CONFIRM:
                msg = "????????";
                break;
            case STATUS_ERROR:
                msg = String.format("???HTTP?(%d)",
                        postEvent.getResponse().code());
                break;
            case CONNECTION_ERROR:
                msg = String.format("???????? (%s)",
                        postEvent.getException().getLocalizedMessage());
                break;
            case SCRIPT_ERROR:
            case UNKNOWN_ERROR:
                msg = "????????";
                break;
            }
            if (!JUtils.isEmpty(msg)) {
                msg = msg + " " + BBSUtils.getPathName(getModel());
                NUtils.announce(msg);
            }
        }
    }
}

/*
    
<html><!-- 2ch_X:cookie --><head><title> ???? </title><META http-equiv="Content-Type" content="text/html; charset=x-sjis"><meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.6,user-scalable=yes" /></head><body bgcolor=#EEEEEE><font size=+1 color=#FF0000><b>?????</b></font><ul><br><br><b> </b><br>??? <br>E-mail <br><br>aaaaaa<br><br></ul><b>?<br>???????????????<br>????????????????<br>???????????21????28?????????????????????????(Race Queen Inc.)??????????????????????????????????<br>?????????????(Race Queen Inc.)????????????????????????<br>????(Race Queen Inc.)???????????????????<br><br></b><form method=POST action="../test/bbs.cgi?guid=ON"><input type=hidden name=subject value=""><input TYPE=hidden NAME=FROM value=""><input TYPE=hidden NAME=mail value=""><input type=hidden name=MESSAGE value="aaaaaa"><input type=hidden name=bbs value=expo><input type=hidden name=sid value=><input type=hidden name=time value=1408795083><input type=hidden name=key value=1341638081><input type=hidden name="yuki" value="akari"><br><input type=submit value="?????" name="submit"><br></form>????????????<br><br>???????????????????????????<br><font size=-1>(cookie?????????????)</font><br></body></html>
 */

/**
    
    
    
?????
    
<html>??<!-- 2ch_X:***** -->?????????
    
????????
    
<!-- 2ch_X:true -->?????
    
?<!-- 2ch_X:false -->???????????
    
<!-- 2ch_X:error -->???
    
????<!-- 2ch_X:check -->???????
    
?<!-- 2ch_X:cookie -->????
    
????????????
    
???????????????????????
    
???????????????????
    
?????????????
    
?????????????????
    
???????????????
    
??????????????
    
?????????
    
<!-- 2ch_X:check -->?????????
    
<!-- 2ch_X:false -->?????????????????
    
*/

/*
    
<html>
<head>
<title>?</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
</head>
<body bgcolor="#FFFFFF"><!-- 2ch_X:error -->
<font size=+1 color=#FF0000><b>??? 8</b></font>
<ul>
<b>p5042-ipngn100102takakise.saga.ocn.ne.jp</B><br><b> </b><br><ul>
??? <b>EXPO'774</b><br>E-mail <br>
<br>??<br>
</ul></ul>
<hr><ul>
<font size=+1 color=#00AA00><b>?????</b></font><ul style="line-height:1.5;">
??????<b>
<a href="http://info.2ch.net/wiki/index.php?%BD%F1%A4%AD%B9%FE%A4%E1%A4%CA%A4%A4%BB%FE%A4%CE%C1%E1%B8%AB%C9%BD">?</a> <a href="http://qb7.2ch.net/_403/madakana.cgi">?</a> <a href="../expo/">??</a></b><br>
</ul><br>
<font size=+1 color=#DD00DD><b>????????</b></font><ul style="line-height:1.5;">
???????<b>?????</b>?????????<br>
?????????????????<br>
<small>????????????<a href="http://ikura.2ch.net/qa/">???</a> <a href="http://engawa.2ch.net/accuse/">?</a> <a href="http://qb5.2ch.net/operate/">?</a> <a href="http://uni.2ch.net/operatex/">?</a> ??????</small><br>
</ul>
</ul></body>
</html>
    
    
    
    
    
    
 */