src/main/java/org/beigesoft/replicator/service/ReplicatorXmlHttps.java
public final Map<String, Integer> makeJob(final URL pUrl,
    final Map<String, Object> pAddParams) throws Exception {
    String requestedDatabaseId = (String) pAddParams.get("requestedDatabaseId");
    String maxRecordsStr = (String) pAddParams.get("maxRecords");
    if (maxRecordsStr == null || maxRecordsStr.length() == 0) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "Where is no maxRecords!!!");
    }
    int maxRecords = Integer.parseInt(maxRecordsStr);
    Map<String, Integer> classesCounts = new LinkedHashMap<String, Integer>();
    Integer classCount = 0;
    boolean isDbPreparedBefore = false;
    int databaseVersion = this.srvDatabase.getVersionDatabase();
    for (Class<?> entityClass : this.mngSettings.getClasses()) {
      int entitiesReceived = 0;
      int firstRecord = 0;
      do {
        // HttpsURLConnection is single request connection
        HttpsURLConnection urlConnection = (HttpsURLConnection) pUrl.openConnection();
        if (!pUrl.getHost().equals(urlConnection.getURL().getHost())) {
          throw new ExceptionWithCode(ExceptionWithCode.SOMETHING_WRONG,
            "You should sign-in in browser first!");
        }
        OutputStreamWriter writer = null;
        BufferedReader reader = null;
        try {
          urlConnection.setDoOutput(true);
          urlConnection.setRequestMethod("POST");
          if (getCookies() != null) {
            urlConnection.addRequestProperty("Cookie", getCookies());
          }
          writer = new OutputStreamWriter(urlConnection
            .getOutputStream(), Charset.forName("UTF-8").newEncoder());
          String nameFilterEntities = this.mngSettings.lazClsSts(entityClass)
            .get("filter");
          String conditions = "";
          if (nameFilterEntities != null) {
            IFilterEntities filterEntities = this.filtersEntities
              .get(nameFilterEntities);
            if (filterEntities != null) {
              String cond = filterEntities.makeFilter(entityClass, pAddParams);
              if (cond != null) {
                conditions = " where " + cond;
              }
            }
          }
          conditions += " limit " + maxRecords + " offset " + firstRecord;
          String requestedDatabaseIdStr = "";
          if (requestedDatabaseId != null) {
            if (Integer.parseInt(requestedDatabaseId)
              == getSrvDatabase().getIdDatabase()) {
              throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
                "requested_database_must_be_different");
            }
            requestedDatabaseIdStr = "&requestedDatabaseId="
                + requestedDatabaseId;
          }
          writer.write("entityName=" + entityClass.getCanonicalName()
            + "&conditions=" + conditions + "&requestingDatabaseVersion="
              + databaseVersion + requestedDatabaseIdStr);
          writer.write("&writerName=" + pAddParams.get("writerName"));
          writer.flush();
          if (HttpsURLConnection.HTTP_OK == urlConnection.getResponseCode()) {
            reader = new BufferedReader(new InputStreamReader(urlConnection
                .getInputStream(), Charset.forName("UTF-8").newDecoder()));
            if (!this.utilXml.readUntilStart(reader, "message")) {
              throw new ExceptionWithCode(ExceptionWithCode.SOMETHING_WRONG,
                "Wrong XML response without message tag!!!");
            }
            Map<String, String> msgAttrsMap = this.srvEntityReaderXml.
              readAttributes(pAddParams, reader);
            String error = msgAttrsMap.get("error");
            if (error != null) {
              throw new ExceptionWithCode(ExceptionWithCode.SOMETHING_WRONG,
                error);
            }
            String entitiesCountStr = msgAttrsMap.get("entitiesCount");
            if (entitiesCountStr == null) {
              throw new ExceptionWithCode(ExceptionWithCode.SOMETHING_WRONG,
                "Wrong XML response without entitiesCount in message!!!");
            }
            entitiesReceived = Integer.parseInt(entitiesCountStr);
            if (entitiesReceived > 0) {
              classCount += entitiesReceived;
              this.logger.info(null, ReplicatorXmlHttp.class,
                "Try to parse entities total: " + entitiesReceived + " of "
                  + entityClass.getCanonicalName());
              if (!isDbPreparedBefore) {
                if (this.databasePrepearerBefore != null) {
                  this.databasePrepearerBefore.make(pAddParams);
                }
                isDbPreparedBefore = true;
              }
              this.databaseReader.readAndStoreEntities(pAddParams, reader);
              if (entitiesReceived == maxRecords) {
                firstRecord += maxRecords;
              } else {
                firstRecord = 0;
                entitiesReceived = 0;
              }
            } else {
              firstRecord = 0;
            }
          } else {
            throw new ExceptionWithCode(ExceptionWithCode.SOMETHING_WRONG,
              "Can't receive data!!! Response code=" + urlConnection
                .getResponseCode());
          }
        } finally {
          if (reader != null) {
            try {
              reader.close();
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
          if (writer != null) {
            try {
              writer.close();
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
          urlConnection.disconnect();
        }
      } while (entitiesReceived > 0);
      classesCounts.put(entityClass.getCanonicalName(), classCount);
      classCount = 0;
    }
    if (this.databasePrepearerAfter != null) {
      this.databasePrepearerAfter.make(pAddParams);
    }
    return classesCounts;
  }
public final Map<String, Integer> makeJob(final URL pUrl,
    final Map<String, Object> pAddParams) throws Exception {
    String requestedDatabaseId = (String) pAddParams.get("requestedDatabaseId");
    String maxRecordsStr = (String) pAddParams.get("maxRecords");
    if (maxRecordsStr == null || maxRecordsStr.length() == 0) {
      throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
        "Where is no maxRecords!!!");
    }
    int maxRecords = Integer.parseInt(maxRecordsStr);
    Map<String, Integer> classesCounts = new LinkedHashMap<String, Integer>();
    Integer classCount = 0;
    boolean isDbPreparedBefore = false;
    int databaseVersion = this.srvDatabase.getVersionDatabase();
    for (Class<?> entityClass : this.mngSettings.getClasses()) {
      int entitiesReceived = 0;
      int firstRecord = 0;
      do {
        // HttpsURLConnection is single request connection
        HttpsURLConnection urlConnection = (HttpsURLConnection) pUrl.openConnection();
        if (!pUrl.getHost().equals(urlConnection.getURL().getHost())) {
          throw new ExceptionWithCode(ExceptionWithCode.SOMETHING_WRONG,
            "You should sign-in in browser first!");
        }
        OutputStreamWriter writer = null;
        BufferedReader reader = null;
        try {
          urlConnection.setDoOutput(true);
          urlConnection.setRequestMethod("POST");
          if (getCookies() != null) {
            urlConnection.addRequestProperty("Cookie", getCookies());
          }
          writer = new OutputStreamWriter(urlConnection
            .getOutputStream(), Charset.forName("UTF-8").newEncoder());
          String nameFilterEntities = this.mngSettings.lazClsSts(entityClass)
            .get("filter");
          String conditions = "";
          if (nameFilterEntities != null) {
            IFilterEntities filterEntities = this.filtersEntities
              .get(nameFilterEntities);
            if (filterEntities != null) {
              String cond = filterEntities.makeFilter(entityClass, pAddParams);
              if (cond != null) {
                conditions = " where " + cond;
              }
            }
          }
          conditions += " limit " + maxRecords + " offset " + firstRecord;
          String requestedDatabaseIdStr = "";
          if (requestedDatabaseId != null) {
            if (Integer.parseInt(requestedDatabaseId)
              == getSrvDatabase().getIdDatabase()) {
              throw new ExceptionWithCode(ExceptionWithCode.WRONG_PARAMETER,
                "requested_database_must_be_different");
            }
            requestedDatabaseIdStr = "&requestedDatabaseId="
                + requestedDatabaseId;
          }
          writer.write("entityName=" + entityClass.getCanonicalName()
            + "&conditions=" + conditions + "&requestingDatabaseVersion="
              + databaseVersion + requestedDatabaseIdStr);
          writer.write("&writerName=" + pAddParams.get("writerName"));
          writer.flush();
          if (HttpsURLConnection.HTTP_OK == urlConnection.getResponseCode()) {
            reader = new BufferedReader(new InputStreamReader(urlConnection
                .getInputStream(), Charset.forName("UTF-8").newDecoder()));
            if (!this.utilXml.readUntilStart(reader, "message")) {
              throw new ExceptionWithCode(ExceptionWithCode.SOMETHING_WRONG,
                "Wrong XML response without message tag!!!");
            }
            Map<String, String> msgAttrsMap = this.srvEntityReaderXml.
              readAttributes(pAddParams, reader);
            String error = msgAttrsMap.get("error");
            if (error != null) {
              throw new ExceptionWithCode(ExceptionWithCode.SOMETHING_WRONG,
                error);
            }
            String entitiesCountStr = msgAttrsMap.get("entitiesCount");
            if (entitiesCountStr == null) {
              throw new ExceptionWithCode(ExceptionWithCode.SOMETHING_WRONG,
                "Wrong XML response without entitiesCount in message!!!");
            }
            entitiesReceived = Integer.parseInt(entitiesCountStr);
            if (entitiesReceived > 0) {
              classCount += entitiesReceived;
              this.logger.info(null, ReplicatorXmlHttp.class,
                "Try to parse entities total: " + entitiesReceived + " of "
                  + entityClass.getCanonicalName());
              if (!isDbPreparedBefore) {
                if (this.databasePrepearerBefore != null) {
                  this.databasePrepearerBefore.make(pAddParams);
                }
                isDbPreparedBefore = true;
              }
              this.databaseReader.readAndStoreEntities(pAddParams, reader);
              if (entitiesReceived == maxRecords) {
                firstRecord += maxRecords;
              } else {
                firstRecord = 0;
                entitiesReceived = 0;
              }
            } else {
              firstRecord = 0;
            }
          } else {
            throw new ExceptionWithCode(ExceptionWithCode.SOMETHING_WRONG,
              "Can't receive data!!! Response code=" + urlConnection
                .getResponseCode());
          }
        } finally {
          if (reader != null) {
            try {
              reader.close();
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
          if (writer != null) {
            try {
              writer.close();
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
          urlConnection.disconnect();
        }
      } while (entitiesReceived > 0);
      classesCounts.put(entityClass.getCanonicalName(), classCount);
      classCount = 0;
    }
    if (this.databasePrepearerAfter != null) {
      this.databasePrepearerAfter.make(pAddParams);
    }
    return classesCounts;
  }
public final Map < String , Integer > makeJob ( final URL pUrl , final Map < String , Object > pAddParams ) throws Exception { String requestedDatabaseId = ( String ) pAddParams . get ( "requestedDatabaseId" ) ; String maxRecordsStr = ( String ) pAddParams . get ( "maxRecords" ) ; if ( maxRecordsStr == null || maxRecordsStr . length ( ) == 0 ) { throw new ExceptionWithCode ( ExceptionWithCode . WRONG_PARAMETER , "Where is no maxRecords!!!" ) ; } int maxRecords = Integer . parseInt ( maxRecordsStr ) ; Map < String , Integer > classesCounts = new LinkedHashMap < String , Integer > ( ) ; Integer classCount = 0 ; boolean isDbPreparedBefore = false ; int databaseVersion = this . srvDatabase . getVersionDatabase ( ) ; for ( Class < ? > entityClass : this . mngSettings . getClasses ( ) ) { int entitiesReceived = 0 ; int firstRecord = 0 ; do { // HttpsURLConnection is single request connection HttpsURLConnection urlConnection = ( HttpsURLConnection ) pUrl . openConnection ( ) ; if ( ! pUrl . getHost ( ) . equals ( urlConnection . getURL ( ) . getHost ( ) ) ) { throw new ExceptionWithCode ( ExceptionWithCode . SOMETHING_WRONG , "You should sign-in in browser first!" ) ; } OutputStreamWriter writer = null ; BufferedReader reader = null ; try { urlConnection . setDoOutput ( true ) ; urlConnection . setRequestMethod ( "POST" ) ; if ( getCookies ( ) != null ) { urlConnection . addRequestProperty ( "Cookie" , getCookies ( ) ) ; } writer = new OutputStreamWriter ( urlConnection . getOutputStream ( ) , Charset . forName ( "UTF-8" ) . newEncoder ( ) ) ; String nameFilterEntities = this . mngSettings . lazClsSts ( entityClass ) . get ( "filter" ) ; String conditions = "" ; if ( nameFilterEntities != null ) { IFilterEntities filterEntities = this . filtersEntities . get ( nameFilterEntities ) ; if ( filterEntities != null ) { String cond = filterEntities . makeFilter ( entityClass , pAddParams ) ; if ( cond != null ) { conditions = " where " + cond ; } } } conditions += " limit " + maxRecords + " offset " + firstRecord ; String requestedDatabaseIdStr = "" ; if ( requestedDatabaseId != null ) { if ( Integer . parseInt ( requestedDatabaseId ) == getSrvDatabase ( ) . getIdDatabase ( ) ) { throw new ExceptionWithCode ( ExceptionWithCode . WRONG_PARAMETER , "requested_database_must_be_different" ) ; } requestedDatabaseIdStr = "&requestedDatabaseId=" + requestedDatabaseId ; } writer . write ( "entityName=" + entityClass . getCanonicalName ( ) + "&conditions=" + conditions + "&requestingDatabaseVersion=" + databaseVersion + requestedDatabaseIdStr ) ; writer . write ( "&writerName=" + pAddParams . get ( "writerName" ) ) ; writer . flush ( ) ; if ( HttpsURLConnection . HTTP_OK == urlConnection . getResponseCode ( ) ) { reader = new BufferedReader ( new InputStreamReader ( urlConnection . getInputStream ( ) , Charset . forName ( "UTF-8" ) . newDecoder ( ) ) ) ; if ( ! this . utilXml . readUntilStart ( reader , "message" ) ) { throw new ExceptionWithCode ( ExceptionWithCode . SOMETHING_WRONG , "Wrong XML response without message tag!!!" ) ; } Map < String , String > msgAttrsMap = this . srvEntityReaderXml . readAttributes ( pAddParams , reader ) ; String error = msgAttrsMap . get ( "error" ) ; if ( error != null ) { throw new ExceptionWithCode ( ExceptionWithCode . SOMETHING_WRONG , error ) ; } String entitiesCountStr = msgAttrsMap . get ( "entitiesCount" ) ; if ( entitiesCountStr == null ) { throw new ExceptionWithCode ( ExceptionWithCode . SOMETHING_WRONG , "Wrong XML response without entitiesCount in message!!!" ) ; } entitiesReceived = Integer . parseInt ( entitiesCountStr ) ; if ( entitiesReceived > 0 ) { classCount += entitiesReceived ; this . logger . info ( null , ReplicatorXmlHttp . class , "Try to parse entities total: " + entitiesReceived + " of " + entityClass . getCanonicalName ( ) ) ; if ( ! isDbPreparedBefore ) { if ( this . databasePrepearerBefore != null ) { this . databasePrepearerBefore . make ( pAddParams ) ; } isDbPreparedBefore = true ; } this . databaseReader . readAndStoreEntities ( pAddParams , reader ) ; if ( entitiesReceived == maxRecords ) { firstRecord += maxRecords ; } else { firstRecord = 0 ; entitiesReceived = 0 ; } } else { firstRecord = 0 ; } } else { throw new ExceptionWithCode ( ExceptionWithCode . SOMETHING_WRONG , "Can't receive data!!! Response code=" + urlConnection . getResponseCode ( ) ) ; } } finally { if ( reader != null ) { try { reader . close ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } if ( writer != null ) { try { writer . close ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } urlConnection . disconnect ( ) ; } } while ( entitiesReceived > 0 ) ; classesCounts . put ( entityClass . getCanonicalName ( ) , classCount ) ; classCount = 0 ; } if ( this . databasePrepearerAfter != null ) { this . databasePrepearerAfter . make ( pAddParams ) ; } return classesCounts ; }
<p>It copy data from another with XML messages
through given HTTP connection.</p>
@param pUrl URL
@param pAddParams additional params
@return Map<String, Integer> affected Class - records count
@throws Exception - an exception
<p > It copy data from another with XML messages through given HTTP connection . < / p >