Sunday, November 12, 2017

Javascript Array intersection

Simple Javascript Array intersection

Example Fiddle

function intersection (...args) {
  if (args.length < 2) return []
  
  const source = args.shift()
  const src = Array.isArray(source) ? source : []
  
  return src.reduce((intersections, value, index, list) => {    
    const intersects = args.reduce((state, array) => {
        return Array.isArray(array) ?
          state && array.indexOf(value) !== -1 :
          state
    }, true)
    
    if (intersects && intersections.indexOf(value) === -1) {
     intersections.push(value)
    }
    
    return intersections
  }, [])
}

Saturday, November 11, 2017

Javascript Promise Reduction

Simple function to perform a reduce on an iterable of Promise/Non-Promise objects.

function promiseReduce (iterable, reducer, initialValue) {
  return iterable.reduce((previousPromise, currentPromise, count) => {
    return Promise.resolve(previousPromise).then(result => {
      return Promise.resolve(currentPromise).then(current => {
        return reducer(result, current, count)
      })
    })
  }, Promise.resolve(initialValue))
}
See fiddle
Or you can use Bluebird.js

Thursday, September 14, 2017

Configuring Netgear Orbi with at&t GigaPower modem


Gigabit internet


I recently upgraded to at&t gigabit internet from a 24 plan. This required me upgrade my current modem. I use quite a bit of port-forwarding for things like plex, lab servers, etc. and had a bit if difficulty getting the new modem to pass all of the traffic to my Orbi so that the Orbi can forward the traffic to the correct hosts.

Here are the steps I took in order to get everything working.

Equipment

  • Netgear Orbi RBR50 + 2 RBS50 satellites
  • ARRIS BGW210-700 Modem/Router aka at&t modem

Basic Setup

WAN <= at&t model <= Orbi Star Network <= LAN

Steps


The at&t modem has a default local address space that conflicts with the Orbi's (192.168.1.0/24). If you plug the Orbi into the router, the Orbi will detect this and change its own address space to 10.0.0.0/24. If this is ok with you, please proceed. However I had a lot of static IPs on 192.168.1.0/24 so I opted to change the at&t modem address space to 192.168.0.0/24.

If your Orbi changed its subnet to 10.0.0.0/24. Go into the Orbi and change it back.



Now make sure the Orbi is plugged into the at&t modem and then set up IP Passthrough in DHCPS-Fixed mode selecting your Orbi router as the device. This will assign the Orbi router the public IP address of the at&t modem



Now log into your Orbi and reboot it.


Once your Orbi is back up the IP Address listed in the INTERNET PORT should no longer be on the 192.168.1.0/24 or 10.0.0.0/24 subnet. If it is you will need to release and renew the INTERNET PORT IP address by clicking the CONNECTION STATUS button which will open a new window where you can RELEASE the IP and then RENEW it. The address should now be the same as the at&t router and port forwarding should work.

Good luck!

Saturday, March 28, 2015

Creating a Jersey WebService In Gatein 3.8.1/JBOSS

I have recently been playing around with GateIn at work. GateIn allows you to build a custom portal based on portlets you can develop in Java. Since it is built on JBOSS it offers things like application clustering and SSO, so its a pretty nice open source product.

However, being new to JBOSS (and java development in general) I ran into a few frustrating issues while trying to deploy a webservice application. After a few days of plugging away at it and reading some helpful blogs I was able to get it working. The issue is that GateIn/JBOSS loads its own implementation of JAX-RS and will not allow you to load any others unless you explicitly say not to in your webapp.

In this blog I will go over how to create a simple REST web service using Jersey that can be deployed and used in GateIn/JBOSS

Prerequisites (what I will be using)

  • Eclipse Luna with Dynamic Web Project Installed
  • GateIn 3.8.1 JBOSS available here
  • Java SDK (The project will need to be set to use 1.7 compatibility)
Project Setup

1. Create a new Dynamic Web Project with the Project name "hellows" and select next


2. Select next again to accept the default output folder
3. Select the checkbox next to Generate web.xml deployment descriptor and leave the rest of the defaults and then select finish to create the project


4. Since we will be using Maven to manage the dependencies, right click on the hellows project and select Configure > Convert to Maven Project
5. In the Create new POM window change the Group Id to com.vbranden (or whatever you like) and leave the rest of the defaults. Then select finish to convert the project

6. Open the hellows/pom.xml file, select the pom.xml tab on the bottom and replace it with the following then save. This will download the dependencies and place them in the project's classpath

pom.xml
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.horiuchi</groupId>  
  <artifactId>testws</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  
  <packaging>war</packaging>  
  <build>  
   <sourceDirectory>src</sourceDirectory>  
   <plugins>  
    <plugin>  
     <artifactId>maven-compiler-plugin</artifactId>  
     <version>3.1</version>  
     <configuration>  
      <source>1.8</source>  
      <target>1.8</target>  
     </configuration>  
    </plugin>  
    <plugin>  
     <artifactId>maven-war-plugin</artifactId>  
     <version>2.4</version>  
     <configuration>  
      <warSourceDirectory>WebContent</warSourceDirectory>  
      <failOnMissingWebXml>false</failOnMissingWebXml>  
     </configuration>  
    </plugin>  
   </plugins>  
  </build>  
  <dependencies>  
       <dependency>  
            <groupId>org.glassfish.jersey.containers</groupId>  
            <artifactId>jersey-container-servlet</artifactId>  
            <version>2.17</version>  
       </dependency>  
       <dependency>  
            <groupId>org.glassfish.jersey.media</groupId>  
            <artifactId>jersey-media-json-jackson</artifactId>  
            <version>2.17</version>  
       </dependency>  
  </dependencies>  
 </project>  

7. Update the web.xml file in hellows/WebContent/WEB-INF and replace it with the following and save

web.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xmlns="http://java.sun.com/xml/ns/javaee"  
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
      id="WebApp_ID" version="3.0">  
      <display-name>hellows</display-name>  
      <servlet>  
           <servlet-name>hellows servlet</servlet-name>  
           <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
           <init-param>  
                <param-name>javax.ws.rs.Application</param-name>  
                <param-value>com.vbranden.HelloApplication</param-value>  
           </init-param>  
           <load-on-startup>1</load-on-startup>  
      </servlet>  
      <servlet-mapping>  
           <servlet-name>hellows servlet</servlet-name>  
           <url-pattern>/*</url-pattern>  
      </servlet-mapping>  
      <context-param>  
           <param-name>resteasy.scan</param-name>  
           <param-value>false</param-value>  
      </context-param>  
      <context-param>  
           <param-name>resteasy.scan.resources</param-name>  
           <param-value>false</param-value>  
      </context-param>  
      <context-param>  
           <param-name>resteasy.scan.providers</param-name>  
           <param-value>false</param-value>  
      </context-param>  
 </web-app>  

8. Now we need to create the actual web service. First we will create the object model for our hellows response. Create a new class under Java Resources/src named HelloObject.java and modify the package to com.vbranden.models. Keep the rest of the defaults


Replace the code for HelloObject.java with the following

HelloObject.java
 package com.vbranden.models;  
 import javax.xml.bind.annotation.XmlElement;  
 import javax.xml.bind.annotation.XmlRootElement;  
 @XmlRootElement(name = "HelloObject")  
 public class HelloObject {  
      private String type;  
      private String message;  
      public HelloObject() {}  
      public HelloObject(String type, String message) {  
           this.setType(type);  
           this.setMessage(message);  
      }  
      /**  
       * @return the message  
       */  
      @XmlElement  
      public String getMessage() {  
           return message;  
      }  
      /**  
       * @param message the message to set  
       */  
      public void setMessage(String message) {  
           this.message = message;  
      }  
      /**  
       * @return the type  
       */  
      @XmlElement  
      public String getType() {  
           return type;  
      }  
      /**  
       * @param type the type to set  
       */  
      public void setType(String type) {  
           this.type = type;  
      }  
 }  

9. Create a new class under Java Resources/src named HelloResource.java and modify the package to com.vbranden.resources. Keep the rest of the defaults


Replace the code for HelloResource.java with the following

HelloResource.java
 package com.vbranden.resources;  
 import javax.ws.rs.GET;  
 import javax.ws.rs.Path;  
 import javax.ws.rs.Produces;  
 import javax.ws.rs.core.MediaType;  
 import javax.ws.rs.core.Response;  
 import com.vbranden.models.HelloObject;  
 @Path("/rest")  
 public class HelloResource {  
      @Path("hello")  
      @GET  
      @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})  
      public Response getHello() {  
           // create a new HelloObject  
           HelloObject hello = new HelloObject("Flanders", "Hi-diddly-ho, neighborino");  
           return Response.ok().entity(hello).build();  
      }  
 }  

10. Next we need to create a class that extends ResourceConfig. This class will point to the package where our resources are. Create a new class under Java Resources/src named HelloApplication.java and modify the package to com.vbranden. Keep the rest of the defaults



Replace the code for HelloApplication.java with the following

HelloApplication.java
 package com.vbranden;  
 import org.glassfish.jersey.server.ResourceConfig;  
 public class HelloApplication extends ResourceConfig {  
      public HelloApplication() {  
           packages("com.vbranden.resources");  
      }  
 }  

11. Finally in order to use this project as a web service in GateIn/JBOSS we need to add the jboss-deployment-structure.xml file to the folder hellows/WebContent/META-INF. Right click the folder and select New > File and create jboss-deployment-structure.xml

Replace the code for jboss-deployment-structure.xml with the following

jboss-deployment-structure.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <jboss-deployment-structure>  
 <deployment>  
 <exclude-subsystems>  
  <subsystem name="resteasy" />  
 </exclude-subsystems>  
 <exclusions>  
  <module name="javaee.api" />  
  <module name="javax.ws.rs.api"/>  
  <module name="org.jboss.resteasy.resteasy-jaxrs" />  
 </exclusions>  
 <local-last value="true" />  
 </deployment>  
 </jboss-deployment-structure>  

12. Now export the project as a WAR file to the directory $GATEIN_HOME/standalone/deployments/hellows.war. GateIn will automatically deploy the web service and it will be ready to test

13. Using a tool like POSTMAN you can quickly check the web service by making a get request to http://127.0.0.1:8080/hellows/rest/hello


Without any options the response will be XML formatted. If you would like a JSON response add an Accept header for application/json


And thats it! Hopefully this will help out those of you with problems getting a JAX-RS webservice to work in GateIn or JBOSS. 

Wednesday, June 25, 2014

VMware SSL certificates made a bit easier

If you have ever replaced certificates on a VMware product you know it can get a little confusing at times. For this reason I have written this simple powershell tool to easily create CSRs and import the replies.

The tool uses a set of canned configuration files for the various VMware products. These configuration files come directly from VMware KB articles. You can also write your own custom cfg files and place them in the templates directory for use in the tool. You must also add a line in the in the vmware-cert-tool.conf file for your custom template defaults.

You can download the tool from here. I have provided a link with OpenSSL included, but google thinks it contains a virus (which it does not)

You can get that here > VMware Certificate Tool w/ OpenSSL

If warnings make you nervous, You can get the package w/o OpenSSL included here > VMware Certificate Tool OpenSSL not Included

You will however need to download and install OpenSSL from http://slproweb.com/products/Win32OpenSSL.html and install it in the vmware cert tool directory under openssl or modify the powershell script to point to your installation

Now on to the screenshots...

When you run the program you will be prompted to either generate a CSR or import a reply

 You will be prompted with a list of all the current configuration templates you have in the templates directory


You will be prompted for the certificate information. In this example we are creating a request for a certificate that will be used for a load balanced SSO 5.5 instance. You can add multiple SANs and IPs

A summary will be printed, if you have made any mistakes you can start over

Several files including the CSR and private key are created. The templateUsed.txt file keeps track of the type of certificate you are requesting

At this point you are on your own to request the certificate from your CA. You can follow VMware's documentation for requesting a certificate here > http://kb.vmware.com/kb/2037432#getcert

For the certificate tool you will only need the .p7b file as this contains the complete certificate chain. For ease place it in the request directory for your request


Start the certificate tool up again and select option 2 to import the reply. You will enter the common name you set for the request and specify the absolute location for the .p7b file


After the tool runs it creates all the certificate files you could ever want including the certificate itself, all of the certificates in the chain, a chain certificate without the host certificate, and even the ever elusive PEM file which contains the entire chain and private key


I hope this helps some of you out.