Package

Source Code of RequestMaker$RequestToAntigate

/**
* automatic sites registrator course project of NSTU students Copyright (C)
* 2012 students of gr. ZF-927(a)
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class RequestMaker {
    enum RequestID {
        ToSearcher, ToAntigate
    }

    public static boolean init ()
    {
        fileContent = new String();

        try {
            BufferedReader in = new BufferedReader(new FileReader("../requests.json"));
            // TODO: �� ������ ������� ����!!!
            String str;

            while ((str = in.readLine()) != null) {
                fileContent += str;
            }

            in.close();
        } catch (FileNotFoundException e) {
            return false;
        } catch (IOException e) {
            return false;
        }

        try {
            json = JSONObject.fromObject(fileContent);
            requestTemplate = json.getJSONObject("template");
        } catch (JSONException e) {
            return false;
        }

        return true;
    }

    public static Request makeRequest (TaskStatus ts, RequestID id)
    {
        switch (id) {
        case ToSearcher:
            try {
                JSONObject searcherData = JSONObject.fromObject(JSONObject.fromObject(json.get(ts.name()))).getJSONObject("first");
                JSONObject tmp = JSONObject.fromObject((Object) requestTemplate);
                Iterator it = searcherData.keys();
                while (it.hasNext()) {
                    String key = (String) it.next();
                    if (tmp.has(key)) {
                        tmp.put(key, searcherData.get(key));
                    } else {
                        ((JSONObject) tmp.get("firstLine")).put(key, searcherData.get(key));
                    }
                }

                RequestToSearcher request = new RequestMaker().new RequestToSearcher(tmp);
                return request;
            } catch (JSONException e) {
                ts.addError("�������������� ���������� �� ����� ������������ �������");
            }
            break;

        case ToAntigate:
            JSONObject requestData = JSONObject.fromObject(json.get("Antigate"));
            RequestToAntigate request = new RequestMaker().new RequestToAntigate(requestData);
            return request;
        }

        return null;
    }

    public static void makeSecondRequest (TaskStatus ts, RequestToSearcher request)
    {
        final String methodKey = "method";
        final String subUrlKey = "subUrl";
        final String contentLength = "Content-Length";
        final String contentType = "Content-Type";
        final String urlTmp = "USER_URL";

        JSONObject secondRequest = JSONObject.fromObject(JSONObject.fromObject(json.get(ts.name()))).getJSONObject("second");

        String method = secondRequest.getString(methodKey);
        String subUrl = secondRequest.getString(subUrlKey);
        String userUrl = UserData.url();
        String lehgth = secondRequest.getString(contentLength);
        String type = secondRequest.getString(contentType);

        subUrl = subUrl.replace(urlTmp, userUrl);
        request.replace(methodKey, method);
        request.replace(subUrlKey, subUrl);
        request.append(contentLength, lehgth);
        request.append(contentType, type);
    }

    /******************************************************************************/
    /** REQUEST **/
    interface Request
    {
        public boolean isHTTPS ();

        public abstract byte[] getBytes ();

        public String getHost ();

    }

    /******************************************************************************/
    /** RequestToSearcher **/
    public class RequestToSearcher implements Request {

        RequestToSearcher(JSONObject obj)
        {
            json = obj;
        }

        public byte[] getBytes ()
        {
            final String POST_LENGTH = "POST_LENGTH";
            String requestAsString = new String();
            JSONObject firstLine = json.getJSONObject("firstLine");

            Iterator it = firstLine.keys();
            while (it.hasNext()) {
                requestAsString += (firstLine.get(it.next()));
                if (it.hasNext()) {
                    requestAsString += " ";
                }
            }
            requestAsString += "\r\n";

            it = json.keys();
            it.next();
            while (it.hasNext()) {
                String key = (String) it.next();
                if (!key.equals(PostData)) {
                    String value = (String) json.get(key);
                    if (!value.equals("") && !key.equals("isHTTPS")) {
                        requestAsString += (key + ": " + json.get(key) + "\r\n");
                    }
                }
            }
            requestAsString += "\r\n";

            int postBegin = requestAsString.length();
            if (json.containsKey(PostData)) {
                JSONObject post = json.getJSONObject(PostData);

                it = post.keys();
                while (it.hasNext()) {
                    String key = (String) it.next();
                    String value = (String) post.get(key);

                    requestAsString += (key + "=" + value);
                    if (it.hasNext()) {
                        requestAsString += "&";
                    }
                }
            }

            if (requestAsString.length() != postBegin) {
                int postLength = requestAsString.length() - postBegin;
                requestAsString = requestAsString.replace(POST_LENGTH, Integer.toString(postLength));
            }

            return requestAsString.getBytes();
        }

        public String getHost ()
        {
            return json.getString("Host");
        }

        public void setPostData (TaskStatus ts, Map<String, String> data, String captcha)
        {
            if (!ts.checkSetError(!data.isEmpty() && !captcha.isEmpty(), "���������� �������� � ������ POST ������: ������ �����������.")) {
                return;
            }

            JSONObject post = new JSONObject();

            post.accumulateAll(data);
            post.put("url", UserData.url());

            final String yandexCaptchaKey = "rep";
            final String mailCaptchaKey = "secure";
            final String ramblerCaptchaKey = "��";
            final String bingCaptchaKey = "userAnswer";
            final String yahooCaptchaKey = "��";
            // TODO: no comments

            String captchaKey = new String();

            switch (ts.name()) {
            case "Mail":
                captchaKey = mailCaptchaKey;
                break;

            case "Yandex":
                captchaKey = yandexCaptchaKey;
                break;

            case "Rambler":
                captchaKey = ramblerCaptchaKey;
                break;

            case "Yahoo":
                captchaKey = yahooCaptchaKey;
                break;

            case "Bing":
                captchaKey = bingCaptchaKey;
                break;
            }

            post.put(captchaKey, captcha);
            json.put(PostData, post);
        }

        public void replace (String key, String val)
        {
            if (json.has(key)) {
                json.put(key, val);
            } else if (json.getJSONObject("firstLine").has(key)) {
                json.getJSONObject("firstLine").put(key, val);
            }
        }

        public void append (String key, String val)
        {
            json.put(key, val);
        }

        public boolean isHTTPS ()
        {
            boolean ssh = json.getBoolean("isHTTPS");
            return ssh;
        }

        private final String PostData = "PostData";
        private JSONObject json;
    }

    /***************************************************************************/
    /** RequestToAntigate **/
    public class RequestToAntigate implements Request {

        public RequestToAntigate(JSONObject obj)
        {
            json = obj;
            imageAsbytes = null;
        }

        public byte[] getBytes ()
        {
            final String BoundaryKey = "BOUNDARY";
            final String BoundaryVal = "---------FGf4Fh3fdjGQ148fdh";
            final String ContentLengthKey = "CONTENT_LENGTH";
            final String UserAGKey = "USER_AG_KEY";
            final String UserAGVal = UserData.agKey();
            final int HeaderLength = 370;

            int contentLength = 1024; // TODO
            String requestAsString = json.getString("header");

            requestAsString = requestAsString.replace(BoundaryKey, BoundaryVal);
            requestAsString = requestAsString.replace(UserAGKey, UserAGVal);
            requestAsString = requestAsString.replace(ContentLengthKey, Integer.toString(contentLength));
            // requestAsString = requestAsString.replace(ContentLengthKey,
            // Integer.toString(HeaderLength + imageAsbytes.length));

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            try {
                outputStream.write(requestAsString.getBytes());
                // outputStream.write(imageAsbytes);
                outputStream.write(new String("\r\n").getBytes());
                outputStream.write(BoundaryVal.getBytes());
                outputStream.write(new String("--").getBytes());
                outputStream.write(new String("\r\n\r\n").getBytes());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("Request = " + new String(outputStream.toByteArray()));
            return outputStream.toByteArray();
        }

        public boolean isHTTPS ()
        {
            return false;
        }

        public String getHost ()
        {
            return host;
        }

        public void addImage (byte[] img)
        {
            imageAsbytes = img;
        }

        private JSONObject json;
        private byte[] imageAsbytes;
        private final String host = "antigate.com";
    }

    /*************************************************************************/
    private static String fileContent;
    private static JSONObject json, requestTemplate;
}
TOP

Related Classes of RequestMaker$RequestToAntigate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.