Enable SQL Server AlwaysOn High Availability

Posted 12/06/2019 10:55 AM by Corey Klass

SQL Server AlwaysOn High Availability is an excellent replication option if you have the need for standby or read-only database servers.

In order to use it in SQL Server Management Studio, you first need to enable it in the SQL Server Configuration Manager, then restart your SQL instance.

1. Open the Start Menu and launch the SQL Server Configuration Manager. Start Menu

2. Select the SQL Server Services option in the left pane, then look for the appropriate SQL Server instance in the right pane. SQL Server Configuration Manager

3. Right-click on the SQL Server instance in the right-pane, and select Properties. SQL Server Instance Selection

4. Click the AlwaysOn High Availability tab. Check the Enable AlwaysOn Availability Groups checkbox if it is not already checked. Enable AlwaysOn

5. When prompted that you need to restart your SQL Server instance, click Yes. Update Confirmation

6. You can then launch the Services administrative tool and restart your SQL Server service to enable AlwaysOn Availability Groups in SQL Server Management Studio.

Angular: Auto-Refresh JWT (JSON Web Token) Authentication

Posted 10/15/2019 9:31 AM by Corey Klass

The applications that I've implemented in Angular 4+ with HttpClient from @angular/common/http utilize JSON Web Tokens (JWT) for authentication from our server, which are then stored in the Angular app and passed in an Authentication header to the web server for any API calls.

This works well until the JWT expires, in my case after two hours, at which point any HTTP requests start failing because of the expired token.

Rather than having to implement JWT refresh code in every HTTP call, I've instead written a drop-in replacement service for HttpClient that performs the refresh authentication automatically. You call the .get() and .post() methods as you would normally for HttpClient.

The full code can be found here, with a breakdown of what's going on below:

import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
import {Observable, Subscriber} from 'rxjs';


@Injectable()

export class CkHttpClientService {

  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  // Populate your own Refresh token value here
  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  refreshTokenValue: string = null;



  // constructor
  constructor(private httpClient: HttpClient) { }



  // is the specified error an auth error?
  private _isAuthError(error: HttpErrorResponse): boolean {
    // default that it's not?
    let authError = false;

    // if an error was included
    if (error) {
      // check the statuses for an auth error
      authError = authError || (error.status === 401);
      authError = authError || (error.status === 403);
    }

    return authError;
  }




  // refreshes the login token
  private _refreshToken(): Observable<boolean> {
    const observable = new Observable((subscriber: Subscriber<boolean>) => {

      // if there is no refresh token value
      if ((this.refreshTokenValue || '') !== '') {
        subscriber.next(false);
        subscriber.complete();

      // if there is a refresh token value
      } else {
        // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        // Your Login Token refresh code goes here
        // - Make sure to store the new refresh token for later use
        // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        this.refreshTokenValue = '';

        subscriber.next(true);
        subscriber.complete();
      }
    });

    return observable;
  }






  // performs a GET operation
  get(url: string, options?: any): Observable<any> {
    return this._http('get', url, null, options);
  }



  // performs a POST operation
  post(url: string, payload: any, options?: any): Observable<any> {
    return this._http('post', url, payload, options);
  }


  // returns the appropriate HttpClient Observable
  private _httpClient(method: string, url: string, payload: any, options: any): Observable<any> {
    // where we're storing our HTTP Client observable
    let httpClientObservable: Observable<any> = null;

    // if this is a POST
    if (method.toLowerCase() === 'post') {
      httpClientObservable = this.httpClient.post(url, payload, options);

      // default to a GET
    } else {
      httpClientObservable = this.httpClient.get(url, options);
    }

    return httpClientObservable;
  }



  // generic HTTP caller
  private _http(method: string, url: string, payload: any, options: any): Observable<any> {
    const observable = Observable.create((subscriber: Subscriber<any>) => {

      // execute the HTTP observable
      this._httpClient(method, url, payload, options).subscribe((result: any) => {
        // on success
        subscriber.next(result);
        subscriber.complete();

      // on error
      }, (error: HttpErrorResponse) => {
        // is this an auth error?
        if (this._isAuthError(error)) {

          // call the refresh token function
          this._refreshToken().subscribe((refreshSuccessFlag: boolean) => {

            // if the refresh was successful
            if (refreshSuccessFlag) {

              // re-attempt the observable
              this._httpClient(method, url, payload, options).subscribe((retryResult: any) => {
                // on success, send the result
                subscriber.next(retryResult);
                subscriber.complete();

              // on error
              }, (retryError: HttpErrorResponse) => {
                subscriber.error(retryError);
                subscriber.complete();
              });

            // if the refresh failed
            } else {
              subscriber.error(error);
              subscriber.complete();
            }
          });

        // if this is not an auth error
        } else {
          subscriber.error(error);
          subscriber.complete();
        }
      });
    });

    return observable;
  }



}

Here is an explanation of what's going on.

refreshTokenValue: string = null;

This is a string variable that stores the login refresh token, and will be used to refresh the token when it expires. You should populate this when the user initially logs in, or you can replace it with a function or getter that refers to another service.

constructor(private httpClient: HttpClient) { }

Here we're injecting httpClient from @angular/common/http.

private _isAuthError(error: HttpErrorResponse): boolean {
  // default that it's not?
  let authError = false;

  // if an error was included
  if (error) {
    // check the statuses for an auth error
    authError = authError || (error.status === 401);
    authError = authError || (error.status === 403);
  }

  return authError;
}

This function determines if the HTTP error is an authentication error. We're using the HTTP responses statuses of 401 (Unauthorized) and 403 (Forbidden). We first check that error is a defined object.

private _refreshToken(): Observable<boolean> {
  const observable = Observable.create((subscriber: Subscriber<boolean>) => {

    // if there is no refresh token value
    if ((this.refreshTokenValue || '') !== '') {
      subscriber.next(false);
      subscriber.complete();

    // if there is a refresh token value
    } else {
      // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      // Your Login Token refresh code goes here
      // - Make sure to store the new refresh token for later use
      // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      this.refreshTokenValue = '';

      subscriber.next(true);
      subscriber.complete();
    }
  });

  return observable;
}

If there is no refresh token stored, the function returns false by default. You'll need to swap out the dummy refresh token code here with your own call to refresh the login token.

get(url: string, options?: any): Observable<any> {
  return this._http('get', url, null, options);
}

post(url: string, payload: any, options?: any): Observable<any> {
  return this._http('post', url, payload, options);
}

These functions mimic HttpClient from @angular/common/http, but internally refer to a common, private _http function, since the code is mostly identical. I haven't implemented OPTIONS or PUT or any other methods, but you should be able to yourself.

private _httpClient(method: string, url: string, payload: any, options: any): Observable<any> {
  // where we're storing our HTTP Client observable
  let httpClientObservable: Observable<any> = null;

  // if this is a POST
  if (method.toLowerCase() === 'post') {
    httpClientObservable = this.httpClient.post(url, payload, options);

  // default to a GET
  } else {
    httpClientObservable = this.httpClient.get(url, options);
  }

  return httpClientObservable;
}

This function returns the appropriate GET or POST Observable, based on the method value passed to it. We'll be using this function multiple times in _http().

private _http(method: string, url: string, payload: any, options: any): Observable<any> {
  const observable = Observable.create((subscriber: Subscriber<any>) => {

The bulk of the heavy lifting is done in this function, performing the initial HTTP request, refreshing the token if we receive an authentication error, and performing the subsequent HTTP request after the token is refreshed.

    this._httpClient(method, url, payload, options).subscribe((result: any) => {
      // on success
      subscriber.next(result);
      subscriber.complete();

    // on error
    }, (error: HttpErrorResponse) => {
      // is this an auth error?
      if (this._isAuthError(error)) {

We first try performing the initial HTTP request. If it was successful, we pass through the result. Otherwise if it errors, we test to see if it was an authentication error.

        // call the refresh token function
        this._refreshToken().subscribe((refreshSuccessFlag: boolean) => {

If it was an authentication error, we call the _refreshToken() function to perform the refresh and return a success indicator.

          // if the refresh was successful
          if (refreshSuccessFlag) {

            // re-attempt the observable
            this._httpClient(method, url, payload, options).subscribe((retryResult: any) => {
              // on success, send the result
              subscriber.next(retryResult);
              subscriber.complete();

If the refresh was successful, we re-attempt the HTTP request that we performed earlier, passing through the result if it was successful.

            // on error
            }, (retryError: HttpErrorResponse) => {
              subscriber.error(retryError);
              subscriber.complete();
            });

If the HTTP request was unsuccessful, we pass through the error.

          // if the refresh failed
          } else {
            subscriber.error(error);
            subscriber.complete();
          }
        });

If the token refresh was not successful, pass through that error.

      // if this is not an auth error
      } else {
        subscriber.error(error);
        subscriber.complete();
      }

If the initial HTTP error wasn't an authentication error, we pass that error through.

Create a Things To-Do from MS Outlook with a Link Back to the Email

Posted 06/27/2019 10:30 AM by Corey Klass

I'm a big fan of using Things by Cultured Code as my To-Do manager, as it's very easy to use and allows for the quick creation and management of my To-Dos.

Since I work almost exclusively off my email in Microsoft Outlook at work, I've created an AppleScript that I've assigned to a hotkey in BetterTouchTool that will automatically generate a new Things To-Do from the selected email.

Once my To-Do is complete, however, it's annoying to go back into MS Outlook to find the email that I was referencing, so that I can respond to it.

To solve this problem, I've added a link to each of these To Do's with a custom URL handler that directs me back to the MS Outlook email that I created the To Do from. The email link that is created in the To Do looks like this

outlookemailopener://2019-05-13%2003:07:17%20+0000/My%20Email%20Subject/ck@malcontentboffin.com

I've written a separate AppleScript, that I've saved as an App and modified the info.plist file to register the outlookemailopener handler as the opener for this AppleScript.

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>Outlook Email Opener</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>outlookemailopener</string>
        </array>
    </dict>
</array>

The outlookemailopener AppleScript uses a Spotlight query to search for the email file on your Mac and open it in MS Outlook. Because it uses a combination of the email date, subject, and sender, rather than a randomly-generated internal identifier, this script will work across all of your Macs (because each copy of MS Outlook assigns an email a randomly generated identifier).

set searchQuery to "(com_microsoft_outlook_messageSent == $time.iso(" & theDate & ")) && (com_microsoft_outlook_author_email_addresses == \"*" & theEmail & "*\") && (kMDItemTitle == \"" & theSubject & "*\")"

set searchCmd to "mdfind '" & searchQuery & "'"

You can find the link to two AppleScripts here.

Bind "Create Things To Do.scpt" to a hotkey that you can press while in MS Outlook to create a Things To-Do.

"Open Outlook Email.app" should be picked up by LaunchServices and register the outlookemailopener URL handler.

Access the Angular-CLI Internal Web Server From Another Computer

Posted 10/29/2018 8:19 PM by Corey Klass

By default, the Angular-CLI internal web server only listens on the localhost network interface (127.0.0.1). If you want to access your development web application from another computer, you can start the internal web server with the following command:

ng serve --host 0.0.0.0 --disableHostCheck true

Run the Angular-CLI Internal Web Server on an Alternate Port

Posted 10/20/2018 10:24 AM by Corey Klass

The Angular-CLI internal web server runs on port 4200 by default. To change the port number, launch it with the following command:

ng serve --port 8888

Path-less SASS Imports in your Angular App

Posted 10/14/2018 3:34 PM by Corey Klass

One of the nice features of SASS in your Angular-CLI app is that you can include an @import "variables"; command to include the SASS variable definitions from an external file. You can easily make your SASS import file globally available for @import statements, so that you only refer to it by filename, by modifying your /angular.json file in the following hierarchy: Root -> "projects" -> "my-project-name" -> "architect" -> "build" -> "options"

Under "options", create a new map entry called "stylePreprocessorOptions" if it does not exist. Within that, create an array entry called "includePaths". Within "includePaths", you can specify the relative path to your SASS file from the root of your project. For example, it may be located at "src/assets/sass".

Global CSS/SASS Includes in Angular-CLI

Posted 10/09/2018 1:34 PM by Corey Klass

You can globally include additional CSS/SASS style sheets into your Angular-CLI application by modifying your /angular.json file in the following hierarchy: Root -> "projects" -> "my-project-name" -> "architect" -> "build" -> "options"

Under "options", create a new array entry called "styles" if it does not exist. Within that, you can specify the relative path to your CSS/SASS file from the root of your project. For example, it may be located at "src/assets/sass".

NPM: Unexpected value 'undefined' exported by the module 'Module1'

Posted 10/02/2018 1:12 PM by Corey Klass

Barrels

It's considered good practice to separate Angular functionality into NPM modules for ease of re-use. The easiest way of importing modules is to import from the root of the module, and you accomplish this by creating a barrel.

In each of your module folders, create an index.ts file that looks like the following:

export * from './file1';
export * from './file2';
export * from './file3';

Then in the root of your module, add export statements for each of the index.ts files that you created above.

export * from './folder1/index';
export * from './folder2/index';

You are then able to import any file definition in your Angular application with a basic module reference:

import {File1} from '@coreyklass/module1';

Errors

Barrels are very useful, but they can result in compilation errors when building your Angular application for production.

The most common of these will be:

ERROR in : Unexpected value 'undefined' exported by the module 'Module1'

Solution

When building your module definition TS file, ensure that all of the import statements reference the actual file that the code is contained in, not the barrel file that you created above. You may not have noticed, but your IDE may have used the barrel for the automatic import statement.

Instead of this in your module definition file: import {File1} from './folder1';

Use this: import {File1} from './folder1/file1';

This should solve your compilation problem.

Grails / CORS / Spring Security Plugin

Posted 04/11/2018 7:05 AM by Corey Klass

It used to be that when using the Spring Security Plugin with Grails, you needed to include in your build.gradle file a separate plugin for handling CORS requests. In more recent versions of Grails, the functionality is built-in, so you only need to add a configuration item in the application.yml file.

In the first grails section, you need to add the following setting:

grails:
    profile: web
    ... (Other Stuff Here)
    cors:
        enabled: true

Set a Timeout on a Block of Code

Posted 04/09/2018 5:51 PM by Corey Klass

Sometimes you have a segment of code that you want to kill if it runs any longer than a specified time period, for example a database query, external API call, or other type of long-running request.

To do this, you want to wrap the block of code to time out in a Callable, and use the ExecutorService to execute the code with a time limit. It will return an array of Future objects, which you can then reference with the .get() method to retrieve the Callable return value.

In your build.gradle file, you will need to include the Grails Executor plug-in if you want to have any GORM/Hibernate functionality in your Callable statement.

    compile "org.grails.plugins:grails-executor:0.4"

You can then use the following code to put a timeout on your executing code. When the timeout is reached, the thread that was spawned is terminated.

    class CallableTestService {

        ExecutorService executorService

        Object performAction() {
            def callable = ({
                // do some logic here
                def resultValue = true

                // return the value
                return resultValue
            } as Callable<Object>)

            def callables = [callable]
            def result = null

            try {
                def timeLimitMS = 1000

                def futures = this.executorService.invokeAll(callables, timeLimitMS, TimeUnit.MILLISECONDS)
                def future = futures?.first()

                result = future.get()

            } catch (error) {
              // error handling goes here
            }


            // do something with the result
            return result
        }
    }