The person in whose behalf the application is made is not entitled to relief from imprisonment or restraint under a writ of habeas corpus, if the time during which such person may be detained legally in custody has not expired, whenever it appears:
1. That the person is detained in custody by virtue of process issued by any court or judge of the United States in a case where such court or judge has exclusive jurisdiction; or2. Except as provided in section 32-22-17, that the person is detained in custody by virtue of the final order or judgment of any competent court of criminal jurisdiction or of any process issued upon such order or judgment.