Extremely Serious

Month: November 2019 (Page 1 of 2)

Writing and Reading Into and From a Text File in Gosu

Writing into a Text File in Gosu

To write into a text file in gosu we can use the FileWriter as follows:

using(var writer = new BufferedWriter(new FileWriter("<FILENAME>"))) {
  writer.write("<TEXT>")  // Writes a text to into a file.
  writer.flush() // Must be at the last.
}
Token Description
FILENAME The target filename to write into.
TEXT The text to write into the filename. You can write as many text as you like. Just don't forget the flush method at the end.

Reading from a Text File in Gosu

To read from a text file in gosu we can use the FileReader with Scanner as follows:

using (var scanner = new Scanner(new BufferedReader(new FileReader("<FILENAME>")))) {
  while(scanner.hasNext()) {
    print(scanner.nextLine()) //---Reads a line from the file.
  }
}
Token Description
FILENAME The target filename to read from.

Using Reduce Method to do AND and OR Testing

If the if conditions are becoming longer and complicated. It is possible to separate all the conditions to a list like the following:

var conditions : List<block() : Boolean>= {
  \-> {
    print("1")
    return true
  }, 
  \-> {
    print("2")
    return false
  }
}

The above example is just a simple illustration and not good for actual coding.

The following code snippet is for hunting at least one truth condition using reduce method to do OR logic testing.

if (conditions?.reduce(false, \ ___aggr, ___cond -> ___aggr || ___cond())) {
  print("I'm in")
}

Once a truth condition was identified it stops checking the rest and the if condition is evaluated to true.

The following code snippet is for checking all the conditions are true using the reduce method to do AND logic testing.

if (conditions?.reduce(true, \ ___aggr, ___cond -> ___aggr && ___cond())) {
  print("I'm in")
}

Once a false condition was identified it stops checking the rest and the if condition is evaluated to false.

Java Proxy Properties By Protocol

Common Protocols

Protocol proxyHost proxyPort nonProxyHosts proxyUser proxyPassword
http Y Y Y Y Y
https Y Y also use http.nonProxyHosts Y Y
ftp Y Y Y Y Y

For nonProxyHosts, a list of hosts that should be reached directly without the proxy. The list is separated by '|' (i.e. pipe symbol). The patterns may start or end with a '*' for wildcards.

If the protocol value is Y for a particular header, it means you can combine the protocol with the header (e.g. http is Y for proxyHost in the table, thus we can combine it as http.proxyHost) to form a single property.

SOCKS Protocol

Property Description
socksProxyHost The host of the SOCKS proxy server.
socksProxyPort The port (i.e. defaulting to 1080) where SOCKS proxy server is listening.
socksProxyVersion The version (i.e. defaulting to 5) of the SOCKS protocol supported by the server.
java.net.socks.username The username for authenticating to SOCKSv5 proxy server.
java.net.socks.password The password for authenticating to SOCKSv5 proxy server.

System Proxy

java.net.useSystemProxies set this property to true to use the system proxy.

Simple Example of Using Some of the Properties

java -Dhttp.proxyHost=localhost -Dhttp.proxyPort=808 ApplicationAccessingTheNet

Extracting GMail SMTP Certificate using OpenSSL

1a.) For TLS, use the following command:

openssl s_client -connect smtp.gmail.com:587 -starttls smtp

1b.) For SSL, use the following command:

openssl s_client -connect smtp.gmail.com:465

2.) Both  outputs of the preceding commands are long but you we are just interested in the entries that starts with:

-----BEGIN CERTIFICATE-----

and ends with:

-----END CERTIFICATE-----

3.) Copy that block of entries to a new file like smtp.gmail.com.pem.

This file is now the certificate file.

Listing the Entries of a Java Keystore

Use the following command to list the entries of a Java keystore:

The keytool is normally found in $JAVA_HOME/jre/bin (i.e. the $JAVA_HOME variable is where you’ve installed JDK).

keytool -list -v -keystore <KEYSTORE_FILE> -storepass <KEYSTORE_PASSWORD>

Include the -a <ALIAS> parameter to just display a single certificate.

Token Description
KEYSTORE_FILE The target key store file (e.g. cacerts found in $JAVA_HOME/jre/lib/security)
KEYSTORE_PASSWORD The password for accessing the keystore (i.e. the default is changeit)

Importing a Certificate to Java Keystore

Use the following command in importing a certificate to Java keystore:

The keytool is normally found in $JAVA_HOME/jre/bin (i.e. the $JAVA_HOME variable is where you've installed JDK).

keytool -importcert -alias <ALIAS> -v -keystore <KEYSTORE_FILE> -file <INPUT_FILE> -storepass <KEYSTORE_PASSWORD>
Token Description
ALIAS Alias name of the entry to process
KEYSTORE_FILE The target key store file (e.g. cacerts found in $JAVA_HOME/jre/lib/security)
INPUT_FILE Input file name (i.e. certificate file like cer, crt or pem)
KEYSTORE_PASSWORD The password for accessing the keystore (i.e. the default is changeit)

 

Using Google SMTP with Archiva in Synology

Pre-requisite

Procedure

  1. Open the archiva.xml file found in the following location:
    /volume1/@appstore/Tomcat7/src/conf/Catalina/localhost
  2. Update the Resource entry with the name mail/Session to become the following:
    <Resource name="mail/Session" auth="Container"
          type="javax.mail.Session"
          mail.smtp.host="smtp.gmail.com"
              mail.smtp.port="587"
              mail.smtp.auth="true"
              mail.smtp.user="<USERNAME>"
              password="<PASSWORD>"
              mail.smtp.starttls.enable="true"
              mail.transport.protocol="smtp"/>
    Token Description
    USERNAME Google Account
    PASSWORD Google App Password
  3. Save the updated file and wait for Archiva to restart.

    If you've updated the archiva.xml file and save it, Tomcat will detect it and it will restart the Archiva application.

Artifactory on Ubuntu with MariaDB

Requirement

  • Java 8
  • MariaDB 10.3.x

Preparing MariaDB

  1. Create the database called artdb using the following command:
    CREATE DATABASE artdb CHARACTER SET utf8 COLLATE utf8_bin;
  2. Add artifactory as the user to the newly created database using the following command:
    GRANT ALL on artdb.* TO 'artifactory'@'<HOST>' IDENTIFIED BY '<PASSWORD>';
    FLUSH PRIVILEGES;
    Token Description
    HOST The address of machine housing MariaDB
    PASSWORD The password for the artifactory user.

Installing Artifactory

  1. Add the artifactory repository to your source list.
    echo "deb https://jfrog.bintray.com/artifactory-debs <DISTRIBUTION> main" | sudo tee -a /etc/apt/sources.list
    Token Description
    DISTRIBUTION Use the following command to identify the destribution:

    lsb_release -c

    The sample output for ubuntu bionic distribution:

    Codename:       bionic
  2. Download jfrog public key using the following command:
    curl https://bintray.com/user/downloadSubjectPublicKey?username=jfrog | sudo apt-key add -
  3. Update your package list using the following command:
    sudo apt-get update
  4. Install the oss artifactory using the following command:
    sudo apt-get install jfrog-artifactory-oss

Artifactory Service Commands

Objective Command
Checking service status sudo service artifactory status
Starting the service sudo service artifactory start
Stopping the service sudo service artifactory stop
Restarting the service sudo service artifactory restart

Accessing The Artifactory from the Browser

  1. Use the following address to access the artifactory application:
    http://localhost:8180/artifactory
  2. Use the following default credentails:
    Field Value
    Username admin
    Password password

    It is recommended to change the admin password after installation but you can do it after hooking it to MariaDB.

Using the MariaDB Prepared Earliear

Variable Value
$ARTIFACTORY_HOME /var/opt/jfrog/artifactory
  1. Using the terminal, change the directory to $ARTIFACTORY_HOME/tomcat/lib and execute the following:
    sudo wget https://downloads.mariadb.com/Connectors/java/connector-java-2.4.4/mariadb-java-client-2.4.4.jar

    You can visit https://mariadb.com/downloads/#connectors for a different version of java client.

  2. Copy $ARTIFACTORY_HOME/misc/db/mariadb.properties to $ARTIFACTORY_HOME/etc/db.properties.

    This will replace the default db.properties that is using derby as the database.

  3. Update the following fields in the db.properties:
    Field Value
    url jdbc:mariadb://<HOST>:<PORT>/artdb?characterEncoding=UTF-8&elideSetAutoCommits=true&useSSL=false&useMysqlMetadata=true
    password The password you've used on Preparing MariaDB section.
    Token Value
    HOST The host you've indentified on Preparing MariaDB section.
    PORT This is the port where MariaDB is listening (i.e. 3306 or 3307).
  4. Restart the artifactory service using the following command:
    sudo service artifactory restart

Creating a Google App Password

Procedure

    1. Sign-in to https://accounts.google.com/ using the account to wanted to use.
    2. Click Security from the side menu
    3. On the Signing in to Google section, click App Passwords.
    4. Select the application you wanted to use from google (e.g. Mail, Calendar, Contacts or YouTube).
    5. Select the Other (Custom name) for the device, and name it appropriately.
    6. Click the generate button.
  1. Now you have your 16-character password that you can use.

« Older posts